﻿using System;
using System.Linq;
using System.Threading;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Inventory;
using Styx.Helpers;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Action = Styx.TreeSharp.Action;

namespace TuanHA_Monk_Special_Edition_Extreme_PvP
{
    public partial class Classname
    {
        #region AvertHarm

        private Composite AvertHarm()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AvertHarm &&
                SpellManager.HasSpell("Avert Harm") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.HealthPercent > THSettings.Instance.UrgentHeal &&
                CountFriendlyNearNeedHeal(Me, 10, THSettings.Instance.AvertHarmHP) >= THSettings.Instance.AvertHarmUnit &&
                GetSpellCooldown("Avert Harm").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Avert Harm", Me, "AvertHarm");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region BlackoutKick

        public DateTime StopDoubleBlackoutKick;

        private Composite BlackoutKick()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackoutKick &&
                StopDoubleBlackoutKick + TimeSpan.FromMilliseconds(500) <= DateTime.Now &&
                SpellManager.HasSpell("Blackout Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 5) &&
                !Me.CurrentTarget.IsPlayer &&
                (Me.CurrentChi >= 4 ||
                 Me.CurrentChi >= 3 &&
                 SpellManager.HasSpell("Fists of Fury") &&
                 GetSpellCooldown("Fists of Fury").TotalMilliseconds > 3000 ||
                 Me.CurrentChi >= 3 &&
                 Me.IsMoving ||
                 Me.CurrentChi >= 2 &&
                 SpellManager.HasSpell("Rising Sun Kick") &&
                 GetSpellCooldown("Rising Sun Kick").TotalMilliseconds > 3000 &&
                 SpellManager.HasSpell("Fists of Fury") &&
                 GetSpellCooldown("Fists of Fury").TotalMilliseconds > 4000 ||
                 Me.CurrentChi >= 2 &&
                 !SpellManager.HasSpell("Rising Sun Kick")) && //Low level
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   CastSpell("Blackout Kick", Me.CurrentTarget, "BlackoutKick");
                                   StopDoubleBlackoutKick = DateTime.Now;
                               })
                );
        }

        #endregion

        #region BlackoutKickShuffle

        private Composite BlackoutKickShuffle()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackoutKick &&
                StopDoubleBlackoutKick + TimeSpan.FromMilliseconds(500) <= DateTime.Now &&
                SpellManager.HasSpell("Blackout Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 5) &&
                MyAuraTimeLeft("Shuffle", Me) < 3000 &&
                Me.CurrentChi >= 2 &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   CastSpell("Blackout Kick", Me.CurrentTarget, "BlackoutKickShuffle");
                                   StopDoubleBlackoutKick = DateTime.Now;
                               })
                );
        }

        #endregion

        #region BlackoutKickBrewDumpChi

        private Composite BlackoutKickBrewDumpChi()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackoutKick &&
                StopDoubleBlackoutKick + TimeSpan.FromMilliseconds(500) <= DateTime.Now &&
                SpellManager.HasSpell("Blackout Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 5) &&
                Me.CurrentChi >= 4 &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   CastSpell("Blackout Kick", Me.CurrentTarget, "BlackoutKickBrewDumpChi");
                                   StopDoubleBlackoutKick = DateTime.Now;
                               })
                );
        }

        #endregion

        #region BlackoutKickPvP

        private Composite BlackoutKickPvP()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackoutKick &&
                StopDoubleBlackoutKick + TimeSpan.FromMilliseconds(500) <= DateTime.Now &&
                SpellManager.HasSpell("Blackout Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi >= Me.MaxChi - 1 &&
                Attackable(Me.CurrentTarget, 5) &&
                Me.CurrentTarget.IsPlayer &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Blackout Kick", Me.CurrentTarget, "BlackoutKickPvP");
                                   StopDoubleBlackoutKick = DateTime.Now;
                               })
                );
        }

        #endregion

        #region BlackoutKickProc

        public DateTime LastBlackoutKickProc;

        private Composite BlackoutKickProc()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackoutKick &&
                LastBlackoutKickProc + TimeSpan.FromMilliseconds(500) <= DateTime.Now &&
                SpellManager.HasSpell("Blackout Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.HasAura("Tiger Power") &&
                Me.GetAuraByName("Tiger Power").TimeLeft.TotalMilliseconds > 2000 &&
                Me.HasAura("Combo Breaker: Blackout Kick") &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Blackout Kick", Me.CurrentTarget, "BlackoutKickProc");
                                   LastBlackoutKickProc = DateTime.Now;
                               })
                );
        }

        #endregion

        #region BlackoutKickMist

        public WoWUnit UnitMeleeMist;

        private bool GetUnitMeleeMist()
        {
            UnitMeleeMist = null;
            UnitMeleeMist = (from unit in NearbyUnFriendlyUnits
                             orderby unit.CurrentHealth ascending
                             where unit.Combat
                             where Me.IsFacing(unit)
                             where Attackable(unit, 5)
                             where
                                 unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                 unit.IsTargetingMyRaidMember
                             select unit).FirstOrDefault();
            return UnitMeleeMist != null;
        }

        private Composite BlackoutKickMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackoutKickMist &&
                SpellManager.HasSpell("Blackout Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                Me.ManaPercent >= THSettings.Instance.BlackoutKickMistMana &&
                (!Me.HasAura("Serpent's Zeal") ||
                 Me.HasAura("Serpent's Zeal") &&
                 Me.GetAuraByName("Serpent's Zeal").StackCount < 2 ||
                 Me.HasAura("Serpent's Zeal") &&
                 Me.GetAuraByName("Serpent's Zeal").TimeLeft.TotalMilliseconds < 10000) &&
                GetUnitMeleeMist(),
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitMeleeMist);

                                   CastSpell("Blackout Kick", UnitMeleeMist, "BlackoutKickMist");
                               })
                );
        }

        private Composite BlackoutKickMistHightPriority()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackoutKickMist &&
                SpellManager.HasSpell("Blackout Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                Me.ManaPercent >= THSettings.Instance.BlackoutKickMistMana &&
                Healable(UnitHeal) &&
                UnitHeal.HealthPercent > THSettings.Instance.PriorityHeal &&
                (!Me.HasAura("Serpent's Zeal") ||
                 Me.HasAura("Serpent's Zeal") &&
                 Me.GetAuraByName("Serpent's Zeal").StackCount < 2 ||
                 Me.HasAura("Serpent's Zeal") &&
                 Me.GetAuraByName("Serpent's Zeal").TimeLeft.TotalMilliseconds < 10000) &&
                GetUnitMeleeMist(),
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitMeleeMist);

                                   CastSpell("Blackout Kick", UnitMeleeMist, "BlackoutKickMist");
                               })
                );
        }

        #endregion

        #region BreathofFire

        public WoWUnit UnitBreathofFire;

        private double CountUnitBreathofFire()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                return
                    NearbyUnFriendlyUnits.Where(
                        unit =>
                        unit.Combat &&
                        Me.IsFacing(unit) &&
                        MyAura("Dizzying Haze", unit) &&
                        (unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                         unit.IsTargetingMeOrPet) &&
                        Attackable(unit, 8)).Aggregate
                        <WoWUnit, double>(0, (current, unit) => current + 1);
            }
        }

        private bool GetUnitBreathofFire()
        {
            UnitBreathofFire = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitBreathofFire = (from unit in NearbyUnFriendlyUnits
                                    where unit.Combat
                                    where CountUnitBreathofFire() >= THSettings.Instance.BreathofFireUnit
                                    orderby CountUnitBreathofFire() descending
                                    select unit).FirstOrDefault();
                return UnitBreathofFire != null;
            }
        }

        private Composite BreathofFire()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BreathofFire &&
                THSettings.Instance.AoE &&
                SpellManager.HasSpell("Breath of Fire") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                GetUnitBreathofFire() &&
                UnitBreathofFire != null &&
                UnitBreathofFire.IsValid,
                new Action(delegate
                               {
                                   ConstantFace(UnitBreathofFire);
                                   CastSpell("Breath of Fire", UnitBreathofFire, "BreathofFire");
                               })
                );
        }

        #endregion

        #region CalculateDropLocation

        public WoWPoint CalculateDropSphereLocation(WoWUnit target)
        {
            WoWPoint dropLocation;

            if (!target.IsMoving || target.MovementInfo.MovingBackward)
            {
                dropLocation = target.Location;
            }
            else
            {
                dropLocation = WoWMathHelper.CalculatePointBehind(target.Location, target.Rotation,
                                                                  -(5 + target.MovementInfo.RunSpeed));
            }

            return dropLocation;
        }

        #endregion

        #region ChargingOxWaveInterrupt

        public WoWUnit UnitChargingOxWaveInterrupt;

        private bool GetUnitChargingOxWaveInterrupt()
        {
            UnitChargingOxWaveInterrupt = null;

            ////using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                {
                    UnitChargingOxWaveInterrupt = (from unit in NearbyUnFriendlyPlayers
                                                   where Attackable(unit, 5)
                                                   where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                                   where FacingOverride(unit)
                                                   where unit.ZDiff < 7
                                                   where
                                                       unit.IsCasting &&
                                                       unit.CurrentCastTimeLeft.TotalMilliseconds <
                                                       THSettings.Instance.LegSweepInterruptMs + MyLatency + 1000 &&
                                                       InterruptCastNoChannel(unit) > MyLatency ||
                                                       unit.IsChanneling &&
                                                       InterruptCastChannel(unit) > MyLatency
                                                   orderby unit.CurrentCastTimeLeft ascending
                                                   select unit).FirstOrDefault();
                }
                    //PvE Search
                else if (!Me.IsInInstance || Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid)
                {
                    UnitChargingOxWaveInterrupt = (from unit in NearbyUnFriendlyUnits
                                                   where Attackable(unit, 5)
                                                   where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                                   where !unit.IsBoss
                                                   where unit.Combat
                                                   where FacingOverride(unit)
                                                   where unit.ZDiff < 7
                                                   where
                                                       unit.IsCasting &&
                                                       unit.CurrentCastTimeLeft.TotalMilliseconds <
                                                       THSettings.Instance.LegSweepInterruptMs + MyLatency + 1000 &&
                                                       unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency ||
                                                       unit.IsChanneling &&
                                                       unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency
                                                   orderby unit.CurrentCastTimeLeft ascending
                                                   select unit).FirstOrDefault();
                }
            }

            return UnitChargingOxWaveInterrupt != null;
        }

        private Composite ChargingOxWaveInterrupt()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.LegSweepInterrupt &&
                    _lastInterrupt + TimeSpan.FromSeconds(1) < DateTime.Now &&
                    SpellManager.HasSpell("Charging Ox Wave") &&
                    !CastingorGCDL() &&
                    GetSpellCooldown("Charging Ox Wave").TotalMilliseconds <= MyLatency &&
                    GetUnitChargingOxWaveInterrupt() &&
                    UnitChargingOxWaveInterrupt != null && UnitChargingOxWaveInterrupt.IsValid,
                    new Action(delegate
                                   {
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }


                                       TrueFacingDelay = DateTime.Now;

                                       if (!Me.IsSafelyFacing(UnitChargingOxWaveInterrupt, 5))
                                       {
                                           while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                                  TimeSpan.FromMilliseconds(
                                                                                      THSettings.Instance
                                                                                                .TrueFacingTimeOut) >
                                                                                  DateTime.Now &&
                                                  !Me.IsSafelyFacing(UnitChargingOxWaveInterrupt, 5))
                                           {
                                               UnitChargingOxWaveInterrupt.Face();
                                               //Logging.Write("ChargingOxWaveInterrupt - Waiting TrueFacing");
                                           }
                                       }

                                       while (UnitChargingOxWaveInterrupt.CurrentCastTimeLeft.TotalMilliseconds >
                                              THSettings.Instance.SpearHandStrikeMs + MyLatency)
                                       {
                                           Logging.Write("Waiting for ChargingOxWaveInterrupt");
                                       }

                                       if (Me.IsSafelyFacing(UnitChargingOxWaveInterrupt, 5) &&
                                           (UnitChargingOxWaveInterrupt.IsCasting ||
                                            UnitChargingOxWaveInterrupt.IsChanneling))
                                       {
                                           CastSpell("Charging Ox Wave", UnitChargingOxWaveInterrupt,
                                                     "ChargingOxWaveInterrupt");
                                           _lastInterrupt = DateTime.Now;
                                       }
                                   })));
        }

        #endregion

        #region ChargingOxWaveEnemyLowHP

        public WoWUnit UnitChargingOxEnemyLowHP;

        private bool GetUnitChargingOxEnemyLowHP()
        {
            UnitChargingOxEnemyLowHP = null;

            UnitChargingOxEnemyLowHP = (from unit in NearbyUnFriendlyPlayers
                                        where !Me.CurrentMap.IsDungeon
                                        where !Me.CurrentMap.IsRaid
                                        where Attackable(unit, 30)
                                        where unit.ZDiff < 7
                                        where !DebuffCC(unit)
                                        where !InvulnerableStun(unit)
                                        where !DebuffSilence(unit)
                                        where unit.HealthPercent <= THSettings.Instance.LegSweepEnemyHP
                                        where unit.HealthPercent > 10
                                        select unit).FirstOrDefault();

            return UnitChargingOxEnemyLowHP != null;
        }

        private Composite ChargingOxWaveEnemyLowHP()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.LegSweepEnemy &&
                    SpellManager.HasSpell("Charging Ox Wave") &&
                    !CastingorGCDL() &&
                    GetUnitChargingOxEnemyLowHP() &&
                    UnitChargingOxEnemyLowHP != null &&
                    UnitChargingOxEnemyLowHP.IsValid &&
                    FacingOverride(UnitChargingOxEnemyLowHP) &&
                    GetSpellCooldown("Charging Ox Wave").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(UnitChargingOxEnemyLowHP);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }

                                       if (!Me.IsSafelyFacing(UnitChargingOxEnemyLowHP, 5))
                                       {
                                           while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                                  TimeSpan.FromMilliseconds(
                                                                                      THSettings.Instance
                                                                                                .TrueFacingTimeOut) >
                                                                                  DateTime.Now &&
                                                  !Me.IsSafelyFacing(UnitChargingOxEnemyLowHP, 5))
                                           {
                                               UnitChargingOxEnemyLowHP.Face();
                                               //Logging.Write("FlyingSerpentKick - Waiting TrueFacing");
                                           }
                                       }
                                       if (Me.IsSafelyFacing(UnitChargingOxEnemyLowHP, 5))
                                       {
                                           CastSpell("Charging Ox Wave", UnitChargingOxEnemyLowHP,
                                                     "ChargingOxWaveEnemyLowHP");
                                       }
                                   })));
        }

        #endregion

        #region ChargingOxWaveMeLowHP

        public WoWUnit UnitChargingOxMeLowHP;

        private bool GetUnitChargingOxMeLowHP()
        {
            UnitChargingOxMeLowHP = null;

            UnitChargingOxMeLowHP = (from unit in NearbyUnFriendlyPlayers
                                     where !Me.CurrentMap.IsDungeon
                                     where !Me.CurrentMap.IsRaid
                                     where Attackable(unit, 30)
                                     where unit.ZDiff < 7
                                     where !DebuffCC(unit)
                                     where !InvulnerableStun(unit)
                                     where unit.GotTarget
                                     where !IsEnemy(unit.CurrentTarget)
                                     where unit.CurrentTarget.HealthPercent <= THSettings.Instance.LegSweepMePercent
                                     select unit).FirstOrDefault();

            if (UnitChargingOxMeLowHP == null)
            {
                UnitChargingOxMeLowHP = (from unit in NearbyUnFriendlyUnits
                                         where !unit.IsBoss
                                         where Attackable(unit, 5)
                                         where !DebuffCC(unit)
                                         where !InvulnerableStun(unit)
                                         where unit.Combat
                                         where
                                             unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                             unit.IsTargetingMyRaidMember
                                         where !IsEnemy(unit.CurrentTarget)
                                         where unit.CurrentTarget.HealthPercent <= THSettings.Instance.LegSweepMePercent
                                         select unit).FirstOrDefault();
            }
            return UnitChargingOxMeLowHP != null;
        }

        private Composite ChargingOxWaveMeLowHP()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.LegSweepMe &&
                    SpellManager.HasSpell("Charging Ox Wave") &&
                    !CastingorGCDL() &&
                    !Me.Buffs.ContainsKey("Touch of Karma") &&
                    GetUnitChargingOxMeLowHP() &&
                    UnitChargingOxMeLowHP != null &&
                    UnitChargingOxMeLowHP.IsValid &&
                    FacingOverride(UnitChargingOxMeLowHP) &&
                    GetSpellCooldown("Charging Ox Wave").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(UnitChargingOxMeLowHP);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }

                                       if (!Me.IsSafelyFacing(UnitChargingOxMeLowHP, 5))
                                       {
                                           while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                                  TimeSpan.FromMilliseconds(
                                                                                      THSettings.Instance
                                                                                                .TrueFacingTimeOut) >
                                                                                  DateTime.Now &&
                                                  !Me.IsSafelyFacing(UnitChargingOxMeLowHP, 5))
                                           {
                                               UnitChargingOxMeLowHP.Face();
                                               //Logging.Write("FlyingSerpentKick - Waiting TrueFacing");
                                           }
                                       }
                                       if (Me.IsSafelyFacing(UnitChargingOxMeLowHP, 5))
                                       {
                                           CastSpell("Charging Ox Wave", UnitChargingOxMeLowHP, "ChargingOxWaveMeLowHP");
                                       }
                                   })));
        }

        #endregion

        #region CheckStance

        public bool CheckStanceState;

        private Composite CheckStance()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    !CheckStanceState &&
                    SpellManager.HasSpell("Stance of the Sturdy Ox") &&
                    !CastingorGCDL() &&
                    !Me.HasAura("Stance of the Sturdy Ox"),
                    new Action(delegate
                                   {
                                       //SafelyFacingTarget(Me.CurrentTarget);
                                       CastSpell("Stance of the Sturdy Ox", Me, "CheckStance");
                                       CheckStanceState = true;
                                   })),
                new Decorator(
                    ret =>
                    !CheckStanceState &&
                    SpellManager.HasSpell("Stance of the Wise Serpent") &&
                    !CastingorGCDL() &&
                    !Me.HasAura("Stance of the Wise Serpent"),
                    new Action(delegate
                                   {
                                       //SafelyFacingTarget(Me.CurrentTarget);
                                       CastSpell("Stance of the Wise Serpent", Me, "CheckStance");
                                       CheckStanceState = true;
                                   }))
                );
        }

        #endregion

        #region ChiBrew

        private Composite ChiBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ChiBrew &&
                SpellManager.HasSpell("Chi Brew") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                _gcdReady + TimeSpan.FromSeconds(4) > DateTime.Now &&
                LastCastSpell != "Jab" &&
                Me.CurrentChi <= THSettings.Instance.ChiBrewCurrentChi &&
                Me.CurrentChi < Me.MaxChi &&
                (Me.CurrentEnergy < 40 ||
                 Me.Specialization == WoWSpec.MonkMistweaver) &&
                GetSpellCooldown("Chi Brew").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Chi Brew", Me, "ChiBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region ChiBurst

        public WoWUnit UnitChiBurstHeal;

        private bool GetUnitChiBurstHeal()
        {
            UnitChiBurstHeal = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitChiBurstHeal = (from unit in NearbyFriendlyUnits
                                    where unit != Me
                                    where unit.Combat
                                    where unit.IsPlayer
                                    where unit.Distance > 8
                                    where Healable(unit)
                                    where unit.HealthPercent <= THSettings.Instance.ChiBurstHP
                                    where
                                        CountFriendlyInLine(unit, 40, THSettings.Instance.ChiBurstHP) >=
                                        THSettings.Instance.ChiBurstUnit
                                    orderby
                                        CountFriendlyInLine(unit, 40, THSettings.Instance.ChiBurstHP) descending
                                    select unit).FirstOrDefault();
                return UnitChiBurstHeal != null;
            }
        }

        public DateTime TrueFacingDelay;
        public DateTime LastChiBurst;
        public WoWUnit LastChiBurstUnit;

        private Composite ChiBurst()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ChiBurst &&
                SpellManager.HasSpell("Chi Burst") &&
                LastChiBurst + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                !Me.Mounted &&
                Me.Combat &&
                !Me.IsMoving &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                GetUnitChiBurstHeal() &&
                UnitChiBurstHeal != null &&
                UnitChiBurstHeal.IsValid,
                new Action(delegate
                               {
                                   CastSpell("Chi Burst", UnitChiBurstHeal, "ChiBurst");
                                   LastChiBurst = DateTime.Now;
                                   LastChiBurstUnit = UnitChiBurstHeal;
                               }));
        }

        #endregion

        #region ChiTorpedo

        private Composite ChiTorpedo()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ChiTorpedo &&
                SpellManager.HasSpell("Chi Torpedo") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 10) &&
                !Me.CurrentTarget.IsFlying &&
                !Me.CurrentTarget.IsPet &&
                Me.IsSafelyFacing(Me.CurrentTarget, 10) &&
                !SpecialUnit.Contains(Me.CurrentTarget.Entry) &&
                CountEneyNearyMeFacing(10) >= THSettings.Instance.ChiTorpedoHP &&
                !DebuffRoot(Me) &&
                GetSpellCooldown("Chi Torpedo").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   InputBlockUse(true, "Roll");

                                   TrueFacingDelay = DateTime.Now;
                                   ConstantFace(Me.CurrentTarget);
                                   if (!Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                   {
                                       while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                              TimeSpan.FromMilliseconds(
                                                                                  THSettings.Instance.TrueFacingTimeOut) >
                                                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                       {
                                           Me.CurrentTarget.Face();
                                           //Logging.Write("ChiTorpedo - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                   {
                                       CastSpell("Roll", Me.CurrentTarget, "ChiTorpedo");
                                   }

                                   LastPull = DateTime.Now;
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region ChiTorpedoMist

        public WoWUnit UnitChiTorpedoMist;

        private bool GetUnitChiTorpedoMist()
        {
            UnitChiTorpedoMist = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitChiTorpedoMist = (from unit in NearbyFriendlyUnits
                                      where unit != Me
                                      where unit.Combat
                                      where unit.IsPlayer
                                      where Me.IsSafelyFacing(unit)
                                      where unit.Distance > 5
                                      where unit.Distance <= 20
                                      where
                                          CountFriendlyInLine(unit, 20, THSettings.Instance.ChiTorpedoMistHP) >=
                                          THSettings.Instance.ChiTorpedoMistUnit
                                      orderby
                                          CountFriendlyInLine(unit, 20, THSettings.Instance.ChiTorpedoMistHP)
                                          descending
                                      select unit).FirstOrDefault();
                return UnitChiTorpedoMist != null;
            }
        }

        private Composite ChiTorpedoMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ChiTorpedoMist &&
                SpellManager.HasSpell("Chi Torpedo") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                GetSpellCooldown("Chi Torpedo").TotalMilliseconds <= MyLatency &&
                GetUnitChiTorpedoMist() &&
                UnitChiTorpedoMist != null &&
                UnitChiTorpedoMist.IsValid,
                new Action(delegate
                               {
                                   TrueFacingDelay = DateTime.Now;
                                   ConstantFace(UnitChiTorpedoMist);
                                   if (!Me.IsSafelyFacing(UnitChiTorpedoMist, 15))
                                   {
                                       while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                              TimeSpan.FromMilliseconds(
                                                                                  THSettings.Instance.TrueFacingTimeOut) >
                                                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(UnitChiTorpedoMist, 15))
                                       {
                                           UnitChiTorpedoMist.Face();
                                           //Logging.Write("ChiTorpedoMist - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(UnitChiTorpedoMist, 15))
                                   {
                                       CastSpell("Roll", UnitChiTorpedoMist, "ChiTorpedoMist");
                                       return RunStatus.Failure;
                                   }
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region ChiWaveMe

        private Composite ChiWaveMe()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ChiWaveMe &&
                SpellManager.HasSpell("Chi Wave") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                Me.HealthPercent <= THSettings.Instance.ChiWaveMeHP &&
                GetSpellCooldown("Chi Wave").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Chi Wave", Me, "ChiWaveMe");
                               })
                );
        }

        #endregion

        #region ChiWave

        private Composite ChiWave()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ChiWave &&
                SpellManager.HasSpell("Chi Wave") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                Healable(UnitHeal) &&
                UnitHeal.HealthPercent <= THSettings.Instance.ChiWaveHP &&
                GetSpellCooldown("Chi Wave").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Chi Wave", UnitHeal, "ChiWave");
                               })
                );
        }

        #endregion

        #region Clash

        public WoWUnit UnitClash;

        private bool GetUnitClash()
        {
            UnitClash = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitClash = (from unit in NearbyUnFriendlyUnits
                             where unit.Combat
                             where unit.Distance > 10
                             where Attackable(unit, 40)
                             where unit.GotTarget
                             where !unit.IsTargetingMeOrPet
                             where !unit.CurrentTarget.HasAura("Vengeance")
                             where unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember
                             select unit).FirstOrDefault();
                return UnitClash != null;
            }
        }

        private Composite Clash()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Clash &&
                SpellManager.HasSpell("Clash") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                GetUnitClash() &&
                UnitClash != null &&
                UnitClash.IsValid &&
                GetSpellCooldown("Clash").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitClash);
                                   CastSpell("Clash", UnitClash, "Clash");
                               })
                );
        }

        #endregion

        #region ClickToMove

        //public void ClickToMoveAction()
        //{
        //    if (ClickToMove < DateTime.Now && Attackable(Me.CurrentTarget))
        //    {
        //        WoWMovement.ClickToMove(Me.CurrentTarget.Location);
        //    }
        //}

        #endregion

        #region CracklingJadeLightningPushBack

        public DateTime LastCracklingJadeLightningPushBack;
        public WoWUnit UnitCracklingJadeLightningPushBack;

        private bool GetUnitCracklingJadeLightningPushBack()
        {
            UnitCracklingJadeLightningPushBack = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitCracklingJadeLightningPushBack = (from unit in NearbyUnFriendlyPlayers
                                                      orderby unit.HealthPercent descending
                                                      where !unit.IsPet
                                                      where FacingOverride(unit)
                                                      where TalentSort(unit) == 1
                                                      where Attackable(unit, 5)
                                                      where unit.GotTarget
                                                      where unit.CurrentTarget == Me
                                                      select unit).FirstOrDefault();
                return UnitCracklingJadeLightningPushBack != null;
            }
        }

        private Composite CracklingJadeLightningPushBack()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.CracklingJadeLightning &&
                    Me.HealthPercent <= THSettings.Instance.CracklingJadeLightningHP &&
                    LastCracklingJadeLightningPushBack + TimeSpan.FromSeconds(9) < DateTime.Now &&
                    SpellManager.HasSpell("Crackling Jade Lightning") &&
                    !Me.IsMoving &&
                    !CastingorGCDL() &&
                    GetUnitCracklingJadeLightningPushBack() &&
                    UnitCracklingJadeLightningPushBack != null &&
                    UnitCracklingJadeLightningPushBack.IsValid &&
                    FacingOverride(UnitCracklingJadeLightningPushBack),
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(UnitCracklingJadeLightningPushBack);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }

                                       CastSpell("Crackling Jade Lightning", UnitCracklingJadeLightningPushBack,
                                                 "CracklingJadeLightningPushBack");
                                       LastCracklingJadeLightningPushBack = DateTime.Now;
                                   })));
        }

        #endregion

        #region CracklingJadeLightningPushBack

        public WoWUnit UnitCracklingJadeLightningPushBackWW;

        private bool GetUnitCracklingJadeLightningPushBackWW()
        {
            UnitCracklingJadeLightningPushBackWW = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitCracklingJadeLightningPushBackWW = (from unit in NearbyUnFriendlyPlayers
                                                        orderby unit.HealthPercent descending
                                                        where FacingOverride(unit)
                                                        where !unit.IsPet
                                                        where unit != Me.CurrentTarget
                                                        where TalentSort(unit) == 1
                                                        where Attackable(unit, 5)
                                                        where unit.GotTarget
                                                        where unit.CurrentTarget == Me
                                                        select unit).FirstOrDefault();
                return UnitCracklingJadeLightningPushBackWW != null;
            }
        }

        private Composite CracklingJadeLightningPushBackWW()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.CracklingJadeLightning &&
                    (Me.CurrentMap.IsBattleground ||
                     Me.CurrentMap.IsArena) &&
                    Me.HealthPercent <= THSettings.Instance.CracklingJadeLightningHP &&
                    LastCracklingJadeLightningPushBack + TimeSpan.FromSeconds(9) < DateTime.Now &&
                    SpellManager.HasSpell("Crackling Jade Lightning") &&
                    !Me.IsMoving &&
                    !CastingorGCDL() &&
                    GetUnitCracklingJadeLightningPushBackWW() &&
                    UnitCracklingJadeLightningPushBackWW != null &&
                    UnitCracklingJadeLightningPushBackWW.IsValid &&
                    FacingOverride(UnitCracklingJadeLightningPushBackWW),
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(UnitCracklingJadeLightningPushBackWW);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }

                                       CastSpell("Crackling Jade Lightning", UnitCracklingJadeLightningPushBackWW,
                                                 "CracklingJadeLightningPushBackWW");
                                       LastCracklingJadeLightningPushBack = DateTime.Now;
                                   })));
        }

        #endregion

        #region CracklingJadeLightningMist

        public WoWUnit UnitCracklingJadeLightningMist;

        private bool GetUnitCracklingJadeLightningMist()
        {
            UnitCracklingJadeLightningMist = null;
            UnitCracklingJadeLightningMist = (from unit in NearbyUnFriendlyUnits
                                              orderby unit.CurrentHealth ascending
                                              where unit.Combat
                                              where !unit.IsPet
                                              where Me.IsFacing(unit)
                                              where Attackable(unit, 40)
                                              where
                                                  unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                                  unit.IsTargetingMyRaidMember
                                              select unit).FirstOrDefault();
            return UnitCracklingJadeLightningMist != null;
        }

        private Composite CracklingJadeLightningMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.CracklingJadeLightningMist &&
                SpellManager.HasSpell("Crackling Jade Lightning") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                (!Me.IsChanneling ||
                 Me.IsChanneling &&
                 Me.ChanneledCastingSpellId != 117952) &&
                Me.CurrentChi < Me.MaxChi &&
                Me.CurrentMana > Me.BaseMana*0.01573 &&
                Me.ManaPercent >= THSettings.Instance.CracklingJadeLightningMistMana &&
                GetUnitCracklingJadeLightningMist(),
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitCracklingJadeLightningMist);

                                   CastSpell("Crackling Jade Lightning", UnitCracklingJadeLightningMist,
                                             "CracklingJadeLightningMist");
                               })
                );
        }

        #endregion

        #region DampenHarm

        public DateTime DampenHamLastCast;

        private Composite DampenHarm()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.DampenHam &&
                SpellManager.HasSpell("Dampen Harm") &&
                DampenHamLastCast + TimeSpan.FromMilliseconds(5000) < DateTime.Now &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.HealthPercent <= THSettings.Instance.DampenHamHP &&
                GetSpellCooldown("Dampen Harm").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Dampen Harm", Me, "DampenHarm");
                                   DampenHamLastCast = DateTime.Now;
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region DetoxFriendlyASAPComp

        private WoWUnit _playerFriendlyDetoxASAP;

        private bool GetPlayerFriendlyDetoxASAP()
        {
            _playerFriendlyDetoxASAP = null;

            /*CombatAdvanced Start*/
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _playerFriendlyDetoxASAP = (from unit in NearbyFriendlyPlayers
                                            orderby TalentSort(unit) descending
                                            where Healable(unit)
                                            where !DebuffDoNotCleanse(unit)
                                            where DebuffCCCleanseASAP(unit)
                                            select unit).FirstOrDefault();
            }
            return _playerFriendlyDetoxASAP != null;
        }

        private Composite DetoxFriendlyASAPComp()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.DetoxCC &&
                    SpellManager.HasSpell("Detox") &&
                    UnitHeal != null &&
                    UnitHeal.IsValid &&
                    UnitHeal.HealthPercent > THSettings.Instance.UrgentHeal &&
                    (Me.IsCasting || !CastingorGCDL()) &&
                    GetPlayerFriendlyDetoxASAP() &&
                    _playerFriendlyDetoxASAP != null &&
                    _playerFriendlyDetoxASAP.IsValid &&
                    GetSpellCooldown("Detox").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       SpellManager.StopCasting();
                                       CastSpell("Detox", _playerFriendlyDetoxASAP);
                                       //Lua.DoString("RunMacroText(\"/s Detox " + PlayerFriendlyDetoxASAP.Name +
                                       //             " ASAP\")");
                                       _playerFriendlyDetoxASAP = null;
                                   }))
                )
                ;
        }

        #endregion

        #region DetoxFriendlyComp

        private WoWUnit _playerFriendlyDetox;

        private bool GetPlayerFriendlyDetox()
        {
            _playerFriendlyDetox = null;

            /*CombatAdvanced Start*/
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _playerFriendlyDetox = (from unit in NearbyFriendlyPlayers
                                        where HealableValid(unit)
                                        where !DebuffDoNotCleanse(unit)
                                        where CountDebuff(unit) >= THSettings.Instance.DetoxNumberDebuff
                                        orderby CountDebuff(unit) descending
                                        select unit).FirstOrDefault();
            }
            /*CombatAdvanced End*/

            return _playerFriendlyDetox != null;
        }

        private Composite DetoxFriendlyComp()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.DetoxDebuff &&
                    SpellManager.HasSpell("Detox") &&
                    UnitHeal != null &&
                    UnitHeal.IsValid &&
                    UnitHeal.HealthPercent > THSettings.Instance.UrgentHeal &&
                    Me.ManaPercent > 30 &&
                    (Me.IsCasting || !CastingorGCDL()) &&
                    GetPlayerFriendlyDetox() &&
                    _playerFriendlyDetox.IsValid &
                    GetSpellCooldown("Detox").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       SpellManager.StopCasting();
                                       CastSpell("Detox", _playerFriendlyDetox);
                                       //Lua.DoString("RunMacroText(\"/s Detox " + PlayerFriendlyDetoxASAP.Name +
                                       //             "\")");
                                       _playerFriendlyDetox = null;
                                   }))
                );
        }

        #endregion

        #region DetoxMe

        private Composite DetoxMe()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.DetoxCC &&
                    (Me.CurrentMap.IsBattleground ||
                     Me.CurrentMap.IsArena) &&
                    Me.ManaPercent > 30 &&
                    !CastingorGCDL() &&
                    DebuffRootCanCleanse(Me) &&
                    !DebuffDoNotCleanse(Me) &&
                    GetSpellCooldown("Detox").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (Me.IsCasting)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       CastSpell("Detox", Me);
                                   }))
                );
        }

        #endregion

        #region DiffuseMagic

        private Composite DiffuseMagic()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.DiffuseMagic &&
                SpellManager.HasSpell("Diffuse Magic") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.HealthPercent <= THSettings.Instance.DiffuseMagicHP &&
                CountDebuffMagic(Me) >= THSettings.Instance.DiffuseMagicDebuff &&
                GetSpellCooldown("Diffuse Magic").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Diffuse Magic", Me, "DiffuseMagic");
                               })
                );
        }

        #endregion

        #region Disable

        public WoWUnit UnitDisable;

        private bool GetUnitDisable()
        {
            UnitDisable = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitDisable = (from unit in NearbyUnFriendlyUnits
                               where Attackable(unit, 5)
                               where FacingOverride(unit)
                               where !InvulnerableRootandSnare(unit)
                               where (unit.IsPlayer || !unit.IsPet && !unit.IsBoss && unit.Fleeing)
                               where !DebuffRootorSnare(unit)
                               where !InvulnerableRootandSnare(unit)
                               orderby TalentSort(unit) ascending
                               select unit).FirstOrDefault();
                return UnitDisable != null;
            }
        }

        public DateTime LastDisable;

        private Composite Disable()
        {
            return new Decorator(
                ret =>
                (THSettings.Instance.Disable && CurrentSpec == "WindWalker" ||
                 THSettings.Instance.DisableMist && CurrentSpec == "MistWeaver" ||
                 THSettings.Instance.DisableBrew && CurrentSpec == "BrewMaster") &&
                //LastDisable + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Disable") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                (Me.CurrentEnergy >= 10 && !Attackable(Me.CurrentTarget, 6) ||
                 Me.CurrentEnergy >= 10 && Attackable(Me.CurrentTarget, 5) && !DebuffRootorSnare(Me.CurrentTarget) ||
                 CurrentSpec == "MistWeaver") &&
                GetUnitDisable() &&
                //Me.CurrentTarget.IsMoving &&
                UnitDisable != null &&
                UnitDisable.IsValid,
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitDisable);
                                   CastSpell("Disable", UnitDisable, "Disable");
                                   LastDisable = DateTime.Now;
                               })
                );
        }

        #endregion

        #region DizzyingHaze

        public WoWUnit UnitDizzyingHaze;

        private bool GetUnitDizzyingHaze()
        {
            UnitDizzyingHaze = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitDizzyingHaze = (from unit in NearbyUnFriendlyUnits
                                    where Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid
                                    where unit.Combat
                                    where !unit.HasAura("Dizzying Haze")
                                    where
                                        LastDizzyingHazeDrop + TimeSpan.FromSeconds(15) < DateTime.Now ||
                                        unit.Location.Distance(LastDizzyingHazePosition) > 8
                                    //Make sure dont aggro extra group
                                    where unit.GotTarget
                                    where (unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember)
                                    where unit.ThreatInfo.RawPercent < 100
                                    where Attackable(unit, 40)
                                    where !Attackable(unit, 8)
                                    select unit).FirstOrDefault();
                return UnitDizzyingHaze != null;
            }
        }

        public WoWPoint LastDizzyingHazePosition;
        public DateTime LastDizzyingHazeDrop;

        private Composite DizzyingHaze()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.DizzyingHaze &&
                THSettings.Instance.AoE &&
                SpellManager.HasSpell("Dizzying Haze") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.EnergyPercent >= 20 &&
                GetUnitDizzyingHaze() &&
                UnitDizzyingHaze != null &&
                UnitDizzyingHaze.IsValid,
                new Action(delegate
                               {
                                   LastDizzyingHazeDrop = DateTime.Now;
                                   CastSpell("Dizzying Haze", UnitDizzyingHaze, "DizzyingHaze");
                                   while (LastDizzyingHazeDrop + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for Dizzying Haze CurrentPendingCursorSpell");
                                   }
                                   LastDizzyingHazePosition = CalculateDropSphereLocation(UnitDizzyingHaze);
                                   SpellManager.ClickRemoteLocation(LastDizzyingHazePosition);
                               })
                );
        }

        #endregion

        #region ElusiveBrew

        private Composite ElusiveBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ElusiveBrew &&
                SpellManager.HasSpell("Elusive Brew") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.Combat &&
                Me.HealthPercent <= THSettings.Instance.ElusiveBrewHP &&
                Me.GetAuraById(128939) != null && //"Elusive Brew" stack
                Me.GetAuraById(115308) == null && //"Elusive Brew" buff
                Attackable(Me.CurrentTarget, 15) &&
                GetSpellCooldown("Elusive Brew").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Elusive Brew", Me, "ElusiveBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region ElusiveBrewStack

        private Composite ElusiveBrewStack()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ElusiveBrewStack &&
                SpellManager.HasSpell("Elusive Brew") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.Combat &&
                Me.GetAuraById(128939) != null && //"Elusive Brew" stack
                Me.GetAuraById(128939).StackCount >= THSettings.Instance.ElusiveBrewStackNumber &&
                //"Elusive Brew" stack
                Me.GetAuraById(115308) == null && //"Elusive Brew" buff
                Attackable(Me.CurrentTarget, 15) &&
                GetSpellCooldown("Elusive Brew").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Elusive Brew", Me, "ElusiveBrewStack");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region EnergizingBrew

        private Composite EnergizingBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.EnergizingBrew &&
                SpellManager.HasSpell("Energizing Brew") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentEnergy <= THSettings.Instance.EnergizingBrewPercent &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget) &&
                GetSpellCooldown("Energizing Brew").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Energizing Brew", Me, "EnergizingBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region ExpelHarm

        private Composite ExpelHarm()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ExpelHarm &&
                SpellManager.HasSpell("Expel Harm") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                (Me.CurrentEnergy >= 40 ||
                 Me.Specialization == WoWSpec.MonkMistweaver &&
                 Me.CurrentMana > Me.BaseMana*0.025) &&
                Me.CurrentChi <= 2 &&
                Me.HealthPercent <= THSettings.Instance.ExpelHarmHP &&
                GetSpellCooldown("Expel Harm").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Expel Harm", Me, "ExpelHarm");
                               })
                );
        }

        #endregion

        #region EnvelopingMist

        private Composite EnvelopingMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.EnvelopingMist &&
                SpellManager.HasSpell("Enveloping Mist") &&
                !Me.Mounted &&
                !Me.IsMoving &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 3 &&
                Healable(UnitHeal) &&
                UnitHeal.Combat &&
                !MyAura("Enveloping Mist", UnitHeal) &&
                UnitHeal.HealthPercent <= THSettings.Instance.EnvelopingMistHP,
                new Action(delegate
                               {
                                   if (!Me.IsChanneling ||
                                       Me.IsChanneling &&
                                       Me.ChanneledCastingSpellId != 115175 ||
                                       Me.IsChanneling &&
                                       Me.ChanneledCastingSpellId == 115175 &&
                                       SoothingMistCurrentTarget != UnitHeal) //Soothing Mist
                                   {
                                       CastSpell("Soothing Mist", UnitHeal, "Soothing Mist before Enveloping Mist");
                                       SoothingMistCurrentTarget = UnitHeal;
                                   }
                                   else
                                   {
                                       CastSpell("Enveloping Mist", UnitHeal, "EnvelopingMist");
                                   }
                                   //SafelyFacingTarget(Me.CurrentTarget);
                               })
                );
        }

        #endregion

        #region FortifyingBrew

        private Composite FortifyingBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.FortifyingBrew &&
                SpellManager.HasSpell("Fortifying Brew") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.HealthPercent <= THSettings.Instance.FortifyingBrewHP &&
                !Me.Buffs.ContainsKey("Touch of Karma") &&
                Attackable(Me.CurrentTarget, 40) &&
                GetSpellCooldown("Fortifying Brew").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Fortifying Brew", Me, "FortifyingBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region FistsofFury

        private Composite FistsofFury()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.FistsofFury &&
                SpellManager.HasSpell("Fists of Fury") &&
                !Me.Mounted &&
                !Me.IsMoving &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 3 &&
                Me.HasAura("Tiger Power") &&
                Me.GetAuraByName("Tiger Power").TimeLeft.TotalMilliseconds > 5000 &&
                Attackable(Me.CurrentTarget, 5 + BonusFistsofFuryRange) &&
                !Me.CurrentTarget.IsPlayer &&
                !Me.CurrentTarget.IsPet &&
                CountEneyNeary(Me, 8) < 3 &&
                FacingOverride(Me.CurrentTarget) &&
                !InvulnerableStun(Me.CurrentTarget) &&
                (Me.CurrentEnergy <= 15 &&
                 LastCastSpell != "Energizing Brew" ||
                 Me.CurrentEnergy <= THSettings.Instance.FistsofFuryEnergy &&
                 LastCastSpell != "Energizing Brew" &&
                 !Me.HasAura("Energizing Brew")) &&
                //GetSpellCooldown("Rising Sun Kick").TotalMilliseconds > 5000 &&
                GetSpellCooldown("Fists of Fury").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   ConstantFace(Me.CurrentTarget);

                                   InputBlockUse(true, "Fists of Fury");
                                   CastSpell("Fists of Fury", Me.CurrentTarget, "FistsofFury");
                               })
                )
                ;
        }

        private Composite FistsofFuryPvP()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.FistsofFury &&
                SpellManager.HasSpell("Fists of Fury") &&
                !IsOverrideModeOn &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 3 &&
                //Me.HasAura("Tiger Power") &&
                //Me.GetAuraByName("Tiger Power").TimeLeft.TotalMilliseconds > 5000 &&
                Attackable(Me.CurrentTarget, 5 + BonusFistsofFuryRange) &&
                Me.CurrentTarget.IsPlayer &&
                FacingOverride(Me.CurrentTarget) &&
                !InvulnerableStun(Me.CurrentTarget) &&
                GetSpellCooldown("Fists of Fury").TotalMilliseconds <= MyLatency,
                //GetSpellCooldown("Rising Sun Kick").TotalMilliseconds > 3000,
                new Action(delegate
                               {
                                   if (!IsOverrideModeOn && Me.IsMoving)
                                   {
                                       WoWMovement.MoveStop();
                                   }
                                   ConstantFace(Me.CurrentTarget);

                                   InputBlockUse(true, "Fists of Fury");
                                   CastSpell("Fists of Fury", Me.CurrentTarget, "FistsofFuryPVP");
                               })
                )
                ;
        }

        public WoWUnit NextFoFTarget;

        private bool GetNextFoFTarget()
        {
            NextFoFTarget = null;
            NextFoFTarget = (from unit in NearbyUnFriendlyUnits
                             orderby unit.HealthPercent ascending
                             where Attackable(unit, 5 + BonusFistsofFuryRange)
                             where
                                 unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                 unit.IsTargetingMyRaidMember || unit.IsPlayer
                             select unit).FirstOrDefault();
            return NextFoFTarget != null;
        }

        public int BonusFistsofFuryRange;

        private void FistsofFuryManager()
        {
            if (Me.IsChanneling && Me.ChanneledSpell.Id == 113656)
            {
                //Logging.Write("Fists of Fury is active");
                if (!Attackable(Me.CurrentTarget, Convert.ToInt16(SpellManager.Spells["Fists of Fury"].MaxRange)))
                {
                    //Logging.Write("Target is Dead or Out of Range");
                    if (GetNextFoFTarget())
                    {
                        NextFoFTarget.Target();
                        ConstantFace(NextFoFTarget);
                        //Logging.Write("Fists of Fury the Next target");
                    }
                    //else
                    //{
                    //    Lua.DoString("RunMacroText(\"/stopcasting\")");
                    //    Logging.Write("Fists of Fury Stop");
                    //}
                }
            }
        }

        #endregion

        #region FlyingSerpentKick

        public DateTime LastPull;

        private Composite FlyingSerpentKick()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.FlyingSerpentKick &&
                LastPull + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Flying Serpent Kick") &&
                HasGlyph.Contains("123403") &&
                LastCastSpell != "Flying Serpent Kick" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 40) &&
                !Me.CurrentTarget.IsMoving &&
                !Me.CurrentTarget.IsFlying &&
                !Me.CurrentTarget.IsPet &&
                Me.IsSafelyFacing(Me.CurrentTarget, 2) &&
                !SpecialUnit.Contains(Me.CurrentTarget.Entry) &&
                !DebuffRoot(Me) &&
                GetDistance(Me.CurrentTarget) >= THSettings.Instance.FlyingSerpentKickMinDistance &&
                GetSpellCooldown("Flying Serpent Kick").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   TrueFacingDelay = DateTime.Now;
                                   InputLasting = DateTime.Now + TimeSpan.FromMilliseconds(500);
                                   InputBlockUse(true, "Flying Serpent Kick");
                                   Me.CurrentTarget.Face();
                                   if (!IsOverrideModeOn && !Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       while (THSettings.Instance.AutoFace &&
                                              TrueFacingDelay +
                                              TimeSpan.FromMilliseconds(THSettings.Instance.TrueFacingTimeOut) >
                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                       {
                                           Me.CurrentTarget.Face();
                                           //Logging.Write("Roll - Waiting TrueFacing");
                                       }
                                   }

                                   if (Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       CastSpell("Flying Serpent Kick", Me.CurrentTarget, "FlyingSerpentKick");
                                   }
                                   LastPull = DateTime.Now;
                               })
                );
        }

        #endregion

        #region GrappleWeapon

        public WoWUnit UnitGrappleWeapon;
        public DateTime LastGrappleWeapon;

        private bool GetUnitGrappleWeapon()
        {
            UnitGrappleWeapon = null;

            UnitGrappleWeapon = (from unit in NearbyUnFriendlyPlayers
                                 orderby unit.CurrentHealth descending
                                 where !Me.CurrentMap.IsDungeon
                                 where !Me.CurrentMap.IsRaid
                                 where Attackable(unit, 40)
                                 where unit.Combat
                                 where FacingOverride(unit)
                                 where TalentSort(unit) <= 2
                                 where unit.GotTarget
                                 where !IsEnemy(unit.CurrentTarget)
                                 where unit.CurrentTarget.HealthPercent <= THSettings.Instance.GrappleWeaponHP
                                 select unit).FirstOrDefault();

            if (UnitGrappleWeapon == null)
            {
                UnitGrappleWeapon = (from unit in NearbyUnFriendlyUnits
                                     orderby unit.CurrentHealth descending
                                     where !Me.CurrentMap.IsBattleground
                                     where !Me.CurrentMap.IsArena
                                     where Attackable(unit, 40)
                                     where unit.Combat
                                     where !unit.IsBoss
                                     where FacingOverride(unit)
                                     where unit.PowerType != WoWPowerType.Mana
                                     where unit.CurrentTarget != null
                                     where
                                         unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                         unit.IsTargetingMyRaidMember
                                     where !IsEnemy(unit.CurrentTarget)
                                     where unit.CurrentTarget.HealthPercent <= THSettings.Instance.GrappleWeaponHP
                                     select unit).FirstOrDefault();
            }
            return UnitGrappleWeapon != null;
        }

        private Composite GrappleWeapon()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.GrappleWeapon &&
                LastGrappleWeapon + TimeSpan.FromSeconds(5) < DateTime.Now &&
                SpellManager.HasSpell("Grapple Weapon") &&
                !CastingorGCDL() &&
                !Me.Mounted &&
                Me.Combat &&
                (GetUnitGrappleWeapon() &&
                 UnitGrappleWeapon != null &&
                 UnitGrappleWeapon.IsValid ||
                 Me.Inventory.Equipped.MainHand == null) &&
                FacingOverride(UnitGrappleWeapon) &&
                GetSpellCooldown("Grapple Weapon").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   LastGrappleWeapon = DateTime.Now;
                                   SafelyFacingTarget(UnitGrappleWeapon);
                                   CastSpell("Grapple Weapon", UnitGrappleWeapon, "GrappleWeapon");
                                   UnitGrappleWeapon = null;
                               })
                );
        }

        #endregion

        #region Guard

        private Composite Guard()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Guard &&
                SpellManager.HasSpell("Guard") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.Combat &&
                Me.CurrentChi >= 2 &&
                Me.HealthPercent <= THSettings.Instance.GuardHP &&
                Attackable(Me.CurrentTarget, 15) &&
                GetSpellCooldown("Guard").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Guard", Me, "Guard");
                               })
                );
        }

        #endregion

        #region GetUnitAttackASAP

        //public WoWUnit UnitAttackASAP;

        //private bool GetUnitAttackASAP()
        //{
        //    if (!Me.CurrentMap.IsArena && !Me.CurrentMap.IsBattleground)
        //    {
        //        return false;
        //    }

        //    UnitAttackASAP = null;

        //    //using (StyxWoW.Memory.AcquireFrame())
        //    {
        //        UnitAttackASAP = (from unit in NearbyUnFriendlyPlayers
        //                          where AttackableValid(unit)
        //                          where UnitAuraTimeLeft("Dominate Mind", unit) > 0
        //                          select unit).FirstOrDefault();
        //    }

        //    if (UnitAttackASAP == null)
        //    {
        //        //using (StyxWoW.Memory.AcquireFrame())
        //        {
        //            UnitAttackASAP = (from unit in NearbyUnFriendlyUnits
        //                              where AttackableValid(unit)
        //                              where unit.IsPet && unit.IsCasting
        //                              where
        //                                  (unit.CastingSpell.Name == "Mesmerize" ||
        //                                   unit.CastingSpell.Name == "Seduction")
        //                              select unit).FirstOrDefault();
        //        }
        //    }

        //    if (UnitAttackASAP == null)
        //    {
        //        //using (StyxWoW.Memory.AcquireFrame())
        //        {
        //            UnitAttackASAP = (from unit in NearbyUnFriendlyPlayers
        //                              where AttackableValid(unit)
        //                              where unit.Class == WoWClass.Rogue || unit.Class == WoWClass.Druid
        //                              where !DebuffDot(unit) && !unit.Combat
        //                              select unit).FirstOrDefault();
        //        }
        //    }

        //    //if (UnitAttackASAP == null)
        //    //{
        //    //    //using (StyxWoW.Memory.AcquireFrame())
        //    //    {
        //    //        UnitAttackASAP = (from unit in NearbyUnFriendlyPlayers
        //    //                          where AttackableValid(unit)
        //    //                          where unit.Class == WoWClass.Mage && unit.HasAura("Invisibility")
        //    //                          select unit).FirstOrDefault();
        //    //    }
        //    //}

        //    if (UnitAttackASAP == null)
        //    {
        //        //using (StyxWoW.Memory.AcquireFrame())
        //        {
        //            UnitAttackASAP = (from unit in NearbyUnFriendlyUnits
        //                              where AttackableValid(unit)
        //                              where
        //                                  (unit.Name == "Spirit Link Totem" || unit.Name == "Earthgrab Totem" ||
        //                                   unit.Name == "Earthbind Totem" || unit.Name.Contains("Static Charge"))
        //                              select unit).FirstOrDefault();
        //        }
        //    }

        //    return UnitAttackASAP != null;
        //}

        #endregion

        #region HealingSphereCombat

        public WoWPoint HealingSphereLocation;

        private Composite HealingSphereCombat()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.DropHealingSphereCombat &&
                (Me.HealthPercent < THSettings.Instance.DropHealingSphereCombatHP ||
                 Me.HealthPercent < 85 &&
                 !Attackable(Me.CurrentTarget, 7)) &&
                LastDropHealingSphere + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Healing Sphere") &&
                Me.Combat &&
                Me.CurrentTarget != null &&
                !CastingorGCDL() &&
                !Attackable(Me.CurrentTarget, 5) &&
                Me.CurrentEnergy >= 35,
                new Action(delegate
                               {
                                   LastDropHealingSphere = DateTime.Now;
                                   CastSpell("Healing Sphere", Me, "HealingSphereCombat");
                                   while (LastDropHealingSphere + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for HealingSphereCombat CurrentPendingCursorSpell");
                                   }

                                   SpellManager.ClickRemoteLocation(CalculateDropSphereLocation(Me));
                               }
                    ));
        }

        #endregion

        #region HealingSphereCombatBeforeReachingEnemy

        private Composite HealingSphereCombatBeforeReachingEnemy()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.DropHealingSphereMovingToEnemy &&
                Me.HealthPercent < THSettings.Instance.DoNotHealAbove &&
                Attackable(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.IsPlayer &&
                Me.CurrentTarget.Distance > 10 &&
                LastDropHealingSphere + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Healing Sphere") &&
                //Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentEnergy >= 80,
                new Action(delegate
                               {
                                   LastDropHealingSphere = DateTime.Now;
                                   CastSpell("Healing Sphere", Me, "HealingSphereCombat");
                                   while (LastDropHealingSphere + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for HealingSphereCombat CurrentPendingCursorSpell");
                                   }

                                   SpellManager.ClickRemoteLocation(CalculateDropSphereLocation(Me.CurrentTarget));
                               }
                    ));
        }

        #endregion

        #region HealingSphereNoCombat

        private Composite HealingSphereNoCombat()
        {
            return new Decorator(
                ret =>
                Me.HealthPercent < 85 &&
                LastDropHealingSphere + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Healing Sphere") &&
                !Me.Combat &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentEnergy >= 80,
                new Action(delegate
                               {
                                   LastDropHealingSphere = DateTime.Now;
                                   CastSpell("Healing Sphere", Me, "HealingSphereNoCombat");
                                   while (LastDropHealingSphere + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for HealingSphereNoCombat CurrentPendingCursorSpell");
                                   }
                                   SpellManager.ClickRemoteLocation(CalculateDropSphereLocation(Me));
                               }
                    ));
        }

        #endregion

        #region HealingSphereUrgent

        public DateTime LastDropHealingSphere;

        private Composite HealingSphereUrgent()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.DropHealingSphereCombat &&
                Me.HealthPercent < THSettings.Instance.UrgentHeal &&
                Me.HealthPercent < THSettings.Instance.DropHealingSphereCombatHP &&
                LastDropHealingSphere + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                (!Attackable(Me.CurrentTarget, 7) ||
                 Attackable(Me.CurrentTarget, 7) && Me.HealthPercent < Me.CurrentTarget.HealthPercent) &&
                SpellManager.HasSpell("Healing Sphere") &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentEnergy >= 80,
                new Action(delegate
                               {
                                   LastDropHealingSphere = DateTime.Now;
                                   CastSpell("Healing Sphere", Me, "HealingSphereUrgent");
                                   while (LastDropHealingSphere + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for HealingSphereUrgent CurrentPendingCursorSpell");
                                   }
                                   SpellManager.ClickRemoteLocation(CalculateDropSphereLocation(Me));
                               }
                    ));
        }

        #endregion

        #region HealingSphereMist

        public WoWPoint HealingSphereLastPosition;

        private Composite HealingSphereMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.HealingSphereMist &&
                (LastDropHealingSphere + TimeSpan.FromMilliseconds(700) < DateTime.Now) &&
                SpellManager.HasSpell("Healing Sphere") &&
                //(!Me.HasAura("Healing Sphere") ||
                // Me.HasAura("Healing Sphere") &&
                // Me.GetAuraByName("Healing Sphere").StackCount < 3) &&
                !CastingorGCDL() &&
                Me.ManaPercent > 10 &&
                Healable(UnitHeal) &&
                (UnitHeal.Location.Distance(HealingSphereLastPosition) > 5 ||
                 LastDropHealingSphere + TimeSpan.FromMilliseconds(2000) < DateTime.Now) &&
                UnitHeal.HealthPercent <= THSettings.Instance.HealingSphereMistHP &&
                !HasHealingSphereNear(UnitHeal, 5),
                new Action(delegate
                               {
                                   LastDropHealingSphere = DateTime.Now;
                                   CastSpell("Healing Sphere", Me, "HealingSphereMist");
                                   while (LastDropHealingSphere + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for HealingSphereMist CurrentPendingCursorSpell");
                                   }
                                   HealingSphereLastPosition = CalculateDropSphereLocation(UnitHeal);
                                   SpellManager.ClickRemoteLocation(HealingSphereLastPosition);
                               }
                    ));
        }

        #endregion

        #region HealingSphereTest

        //private Composite HealingSphereTest()
        //{
        //    return new Decorator(
        //        ret =>
        //        LastDropHealingSphere + TimeSpan.FromMilliseconds(700) < DateTime.Now &&
        //        SpellManager.HasSpell("Healing Sphere") &&
        //        //(!Me.HasAura("Healing Sphere") ||
        //        // Me.HasAura("Healing Sphere") &&
        //        // Me.GetAuraByName("Healing Sphere").StackCount < 3) &&
        //        !CastingorGCDL() &&
        //        Me.ManaPercent > 10 &&
        //        Me.CurrentTarget != null &&
        //        Me.CurrentTarget.Distance < 40,
        //        new Action(delegate
        //                       {
        //                           LastDropHealingSphere = DateTime.Now;
        //                           CastSpell("Healing Sphere", Me, "HealingSphereTest");
        //                           while (LastDropHealingSphere + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
        //                                  Me.CurrentPendingCursorSpell == null)
        //                           {
        //                               Logging.Write("Waiting for Healing Sphere CurrentPendingCursorSpell");
        //                           }
        //                           HealingSphereLastPosition =
        //                               WoWMathHelper.CalculatePointFrom(Me.Location,
        //                                                                Me.CurrentTarget.Location,
        //                                                                (float) Me.CurrentTarget.Distance - 5);
        //                           SpellManager.ClickRemoteLocation(HealingSphereLastPosition);
        //                       }
        //            ));
        //}

        #endregion

        #region HealingSphereMistTank

        private bool HasHealingSphereNear(WoWUnit unit, float distance)
        {
            HealingSphere = null;

            HealingSphere = ObjectManager.ObjectList.FirstOrDefault(o => o.Type == WoWObjectType.AreaTrigger &&
                                                                         o.Entry == 2866 &&
                                                                         o.Location.Distance(unit.Location) <= distance);

            return HealingSphere != null;
        }

        public WoWUnit UnitHealingSphereMistTank;

        private bool GetUnitHealingSphereMistTank()
        {
            UnitHealingSphereMistTank = null;
            UnitHealingSphereMistTank = (from unit in NearbyFriendlyUnits
                                         orderby unit.CurrentHealth ascending
                                         where unit.HasAura("Vengeance")
                                         where Attackable(unit.CurrentTarget)
                                         where
                                             unit.CurrentTarget.IsTargetingMeOrPet ||
                                             unit.CurrentTarget.IsTargetingMyPartyMember ||
                                             unit.CurrentTarget.IsTargetingMyRaidMember
                                         where !HasHealingSphereNear(unit, 5)
                                         select unit).FirstOrDefault();
            return UnitHealingSphereMistTank != null;
        }

        public DateTime LastDropHealingSphereMistTank;

        private Composite HealingSphereMistTank()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.HealingSphereMistTank &&
                LastDropHealingSphereMistTank +
                TimeSpan.FromSeconds(THSettings.Instance.HealingSphereMistTankMs) < DateTime.Now &&
                SpellManager.HasSpell("Healing Sphere") &&
                (!Me.HasAura("Healing Sphere") ||
                 Me.HasAura("Healing Sphere") &&
                 Me.GetAuraByName("Healing Sphere").StackCount < 3) &&
                !CastingorGCDL() &&
                Me.ManaPercent > 10 &&
                GetUnitHealingSphereMistTank() &&
                UnitHealingSphereMistTank != null &&
                UnitHealingSphereMistTank.IsValid,
                new Action(delegate
                               {
                                   LastDropHealingSphereMistTank = DateTime.Now;
                                   CastSpell("Healing Sphere", Me, "HealingSphereMistTank");
                                   while (LastDropHealingSphereMistTank + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for HealingSphereMistTank CurrentPendingCursorSpell");
                                   }
                                   HealingSphereLastPosition = CalculateDropSphereLocation(UnitHeal);
                                   SpellManager.ClickRemoteLocation(HealingSphereLastPosition);
                               }
                    ));
        }

        #endregion

        #region Interrupt

        private static DateTime _lastInterrupt;

        //private WoWUnit _unitInterrupt;

        //private bool GetUnitInterrupt(int distance, double baseCastTime, double atCastTimeLeft, bool facingRequirement,
        //                              bool interruptableRequirement)
        //{
        //    _unitInterrupt = null;

        //    ////using (StyxWoW.Memory.AcquireFrame())
        //    {
        //        if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
        //        {
        //            _unitInterrupt = (from unit in NearbyUnFriendlyPlayers
        //                              where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
        //                              orderby unit.CurrentCastTimeLeft ascending
        //                              where
        //                                  InterruptCastNoChannel(unit) > baseCastTime &&
        //                                  InterruptCastNoChannel(unit) < 1500 + atCastTimeLeft ||
        //                                  InterruptCastChannel(unit) > 0
        //                              where
        //                                  unit.CanInterruptCurrentSpellCast ||
        //                                  !unit.CanInterruptCurrentSpellCast &&
        //                                  !interruptableRequirement
        //                              where Attackable(unit, distance)
        //                              where
        //                                  Me.IsFacing(unit) ||
        //                                  FacingOverride(unit) ||
        //                                  !Me.IsFacing(unit) && !facingRequirement
        //                              where unit.InLineOfSpellSight
        //                              select unit).FirstOrDefault();
        //        }
        //            //PvE Search
        //        else if (!Me.IsInInstance || Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid)
        //        {
        //            _unitInterrupt = (from unit in NearbyUnFriendlyUnits
        //                              where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
        //                              where unit.IsCasting || unit.IsChanneling
        //                              orderby unit.CurrentCastTimeLeft ascending
        //                              where !unit.IsBoss
        //                              where unit.Combat
        //                              where
        //                                  unit.IsTargetingMeOrPet ||
        //                                  unit.IsTargetingMyPartyMember ||
        //                                  unit.IsTargetingMyRaidMember
        //                              where
        //                                  unit.CanInterruptCurrentSpellCast ||
        //                                  !unit.CanInterruptCurrentSpellCast &&
        //                                  !interruptableRequirement
        //                              where
        //                                  unit.CurrentCastTimeLeft.TotalMilliseconds > baseCastTime ||
        //                                  unit.IsChanneling
        //                              where
        //                                  unit.CurrentCastTimeLeft.TotalMilliseconds <
        //                                  1500 + atCastTimeLeft || unit.IsChanneling
        //                              where Attackable(unit, distance)
        //                              where
        //                                  Me.IsFacing(unit) ||
        //                                  FacingOverride(unit) ||
        //                                  !Me.IsFacing(unit) && !facingRequirement
        //                              select unit).FirstOrDefault();
        //        }
        //    }

        //    return _unitInterrupt != null;
        //}

        #endregion

        #region InvokeXuen

        private Composite InvokeXuen()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.InvokeXuen &&
                SpellManager.HasSpell("Invoke Xuen, the White Tiger") &&
                !CastingorGCDL() &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 10) &&
                !Me.CurrentTarget.Mounted &&
                Me.CurrentTarget.HealthPercent <= THSettings.Instance.InvokeXuenHP &&
                (Me.CurrentTarget.CurrentHealth > Me.CurrentHealth - (Me.CurrentHealth*0.2) ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                (THSettings.Instance.InvokeXuenAll ||
                 THSettings.Instance.Burst ||
                 THSettings.Instance.InvokeXuenBoss && Me.CurrentTarget.IsBoss ||
                 THSettings.Instance.InvokeXuenPlayer && Me.CurrentTarget.IsPlayer ||
                 THSettings.Instance.InvokeXuenElite && Me.CurrentTarget.Elite ||
                 THSettings.Instance.InvokeXuenTank && Me.HealthPercent <= THSettings.Instance.InvokeXuenTankHP) &&
                GetSpellCooldown("Invoke Xuen, the White Tiger").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Invoke Xuen, the White Tiger", Me.CurrentTarget, "InvokeXuen");
                               })
                )
                ;
        }

        #endregion

        #region JabCapEnergy

        public DateTime JabLast;

        private Composite JabCapEnergy()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Jab &&
                JabLast + TimeSpan.FromMilliseconds(500) < DateTime.Now &&
                SpellManager.HasSpell("Jab") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                //It is better to use a mastery proc and energy cap, than it is to Jab before using a Mastery Proc. 
                //Otherwise, using Jab while you already have a mastery proc active is considered a damage loss.
                !Me.HasAura("Combo Breaker: Tiger Palm") &&
                !Me.HasAura("Combo Breaker: Blackout Kick") &&
                (Me.CurrentEnergy >= 90 ||
                 Me.CurrentEnergy >= 30 && !SpellManager.HasSpell("Tiger Palm")) && //Low level lol
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Jab", Me.CurrentTarget, "JabCapEnergy");
                                   JabLast = DateTime.Now;
                               })
                );
        }

        #endregion

        #region JabNearCapEnergy

        private Composite JabNearCapEnergy()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Jab &&
                JabLast + TimeSpan.FromMilliseconds(500) < DateTime.Now &&
                SpellManager.HasSpell("Jab") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                //CurrentSpec!="MistWeaver" &&
                //It is better to use a mastery proc and energy cap, than it is to Jab before using a Mastery Proc. 
                //Otherwise, using Jab while you already have a mastery proc active is considered a damage loss.
                //!Me.HasAura("Combo Breaker: Tiger Palm") &&
                //!Me.HasAura("Combo Breaker: Blackout Kick") &&
                (Me.CurrentEnergy >= 70 && Me.CurrentChi < Me.MaxChi - 1 ||
                 Me.CurrentEnergy >= 30 && !SpellManager.HasSpell("Tiger Palm")) && //Low level lol
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Jab", Me.CurrentTarget, "JabNearCapEnergy");
                                   JabLast = DateTime.Now;
                               })
                );
        }

        #endregion

        #region JabBrewNearCapEnergy

        private Composite JabBrewNearCapEnergy()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Jab &&
                JabLast + TimeSpan.FromMilliseconds(500) < DateTime.Now &&
                SpellManager.HasSpell("Jab") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                //CurrentSpec!="MistWeaver" &&
                //It is better to use a mastery proc and energy cap, than it is to Jab before using a Mastery Proc. 
                //Otherwise, using Jab while you already have a mastery proc active is considered a damage loss.
                //!Me.HasAura("Combo Breaker: Tiger Palm") &&
                //!Me.HasAura("Combo Breaker: Blackout Kick") &&
                (Me.CurrentEnergy >= 70 && Me.CurrentChi < Me.MaxChi ||
                 Me.CurrentEnergy >= 30 && !SpellManager.HasSpell("Tiger Palm")) && //Low level lol
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Jab", Me.CurrentTarget, "JabBrewNearCapEnergy");
                                   JabLast = DateTime.Now;
                               })
                );
        }

        #endregion

        #region Jab

        private Composite Jab()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Jab &&
                JabLast + TimeSpan.FromMilliseconds(700) < DateTime.Now &&
                SpellManager.HasSpell("Jab") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentEnergy >= 30 &&
                Me.CurrentChi <= Me.MaxChi - 2 &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Jab", Me.CurrentTarget, "Jab");
                                   JabLast = DateTime.Now;
                               })
                );
        }

        #endregion

        #region JabBrew

        private Composite JabBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.JabBrew &&
                JabLast + TimeSpan.FromMilliseconds(700) < DateTime.Now &&
                SpellManager.HasSpell("Jab") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                (Me.CurrentEnergy >= 70 ||
                 GetSpellCooldown("Keg Smash").TotalMilliseconds > 2000 &&
                 Me.CurrentEnergy >= 30) &&
                Me.CurrentChi <= Me.MaxChi - 2 &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Jab", Me.CurrentTarget, "JabBrew");
                                   JabLast = DateTime.Now;
                               })
                );
        }

        #endregion

        #region JabBrewCapEnergy

        private Composite JabBrewCapEnergy()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.JabBrew &&
                JabLast + TimeSpan.FromMilliseconds(700) < DateTime.Now &&
                SpellManager.HasSpell("Jab") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentEnergy >= 95 &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Jab", Me.CurrentTarget, "JabBrewCapEnergy");
                                   JabLast = DateTime.Now;
                               })
                );
        }

        #endregion

        #region JabMist

        public WoWUnit UnitJabMist;

        private bool GetUnitJabMist()
        {
            UnitJabMist = null;
            UnitJabMist = (from unit in NearbyUnFriendlyUnits
                           orderby unit.CurrentHealth ascending
                           where unit.Combat
                           where Me.IsFacing(unit)
                           where Attackable(unit, 5)
                           where
                               unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                               unit.IsTargetingMyRaidMember
                           select unit).FirstOrDefault();
            return UnitJabMist != null;
        }

        private Composite JabMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.JabMist &&
                SpellManager.HasSpell("Jab") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi < THSettings.Instance.JabMistChi &&
                Me.ManaPercent >= THSettings.Instance.JabMistMana &&
                Me.CurrentMana > Me.BaseMana*0.03 &&
                GetUnitJabMist(),
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitJabMist);

                                   CastSpell("Jab", UnitJabMist, "JabMist");
                               })
                );
        }

        #endregion

        #region KegSmashDebuff

        public WoWUnit UnitKegSmash;

        private bool GetUnitUnitKegSmash()
        {
            UnitKegSmash = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitKegSmash = (from unit in NearbyUnFriendlyUnits
                                where FacingOverride(unit)
                                where !unit.HasAura("Weakended Blows")
                                where Attackable(unit, 5)
                                select unit).FirstOrDefault();
                return UnitKegSmash != null;
            }
        }

        private Composite KegSmashDebuff()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.KegSmash &&
                SpellManager.HasSpell("Keg Smash") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.EnergyPercent >= 35 &&
                Me.CurrentChi < Me.MaxChi - 1 &&
                GetUnitUnitKegSmash() &&
                UnitKegSmash != null &&
                UnitKegSmash.IsValid &&
                GetSpellCooldown("Keg Smash").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   TrueFacingDelay = DateTime.Now;
                                   ConstantFace(UnitKegSmash);
                                   if (!Me.IsSafelyFacing(UnitKegSmash, 45))
                                   {
                                       while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                              TimeSpan.FromMilliseconds(
                                                                                  THSettings.Instance.TrueFacingTimeOut) >
                                                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(UnitKegSmash, 45))
                                       {
                                           UnitKegSmash.Face();
                                           //Logging.Write("KegSmashDebuff - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(UnitKegSmash, 45))
                                   {
                                       CastSpell("Keg Smash", UnitKegSmash, "KegSmashDebuff");
                                   }
                               })
                );
        }

        #endregion

        #region KegSmashAoE

        private Composite KegSmashAoE()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.KegSmash &&
                THSettings.Instance.AoE &&
                SpellManager.HasSpell("Keg Smash") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 5) &&
                Me.EnergyPercent >= 35 &&
                Me.CurrentChi < Me.MaxChi - 1 &&
                CountEneyNearyMeFacing(8) >= THSettings.Instance.KegSmashUnit &&
                GetSpellCooldown("Keg Smash").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   TrueFacingDelay = DateTime.Now;
                                   ConstantFace(UnitKegSmash);
                                   if (!Me.IsSafelyFacing(UnitKegSmash, 45))
                                   {
                                       while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                              TimeSpan.FromMilliseconds(
                                                                                  THSettings.Instance.TrueFacingTimeOut) >
                                                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(UnitKegSmash, 45))
                                       {
                                           UnitKegSmash.Face();
                                           //Logging.Write("KegSmashAoE - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(UnitKegSmash, 45))
                                   {
                                       CastSpell("Keg Smash", UnitKegSmash, "KegSmashAoE");
                                   }
                               })
                );
        }

        #endregion

        #region LeeroftheOxBrew

        public WoWUnit UnitLeeroftheOx;

        private bool GetUnitLeeroftheOx()
        {
            UnitLeeroftheOx = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitLeeroftheOx = (from unit in NearbyUnFriendlyUnits
                                   where unit.Combat
                                   where Attackable(unit)
                                   where SummonBlackOxStatueLastPosition.Distance(unit.Location) < 40
                                   where unit.GotTarget
                                   where !unit.IsTargetingMeOrPet
                                   where !unit.CurrentTarget.HasAura("Vengeance")
                                   where unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember
                                   select unit).FirstOrDefault();
                return UnitLeeroftheOx != null;
            }
        }

        private Composite LeeroftheOxBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.LeeroftheOxBrew &&
                SpellManager.HasSpell("Leer of the Ox") &&
                LastCastSpell != "Leer of the Ox" &&
                LastCastSpell != "Provoke" &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                GetUnitLeeroftheOx() &&
                UnitLeeroftheOx != null &&
                UnitLeeroftheOx.IsValid &&
                GetSpellCooldown("Leer of the Ox").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   CastSpell("Leer of the Ox", UnitLeeroftheOx, "LeeroftheOxBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region LegacyoftheEmperor

        private Composite LegacyoftheEmperor()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AutoBuff &&
                SpellManager.HasSpell("Legacy of the Emperor") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentEnergy >= 10 &&
                !Me.HasAura("Legacy of the Emperor") &&
                !Me.HasAura("Blessing of Kings") &&
                !Me.HasAura("Mark of the Wild") &&
                !Attackable(Me.CurrentTarget, 10),
                new Action(delegate { CastSpell("Legacy of the Emperor", Me, "LegacyoftheEmperor "); })
                );
        }

        #endregion

        #region LegacyoftheEmperorGroupBuff

        private Composite LegacyoftheEmperorGroupBuff()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AutoBuff &&
                SpellManager.HasSpell("Legacy of the Emperor") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Healable(UnitHeal) &&
                UnitHeal.IsPlayer &&
                !UnitHeal.HasAura("Legacy of the Emperor") &&
                !UnitHeal.HasAura("Blessing of Kings") &&
                !UnitHeal.HasAura("Mark of the Wild") &&
                !Attackable(Me.CurrentTarget, 10),
                new Action(delegate { CastSpell("Legacy of the Emperor", Me, "LegacyoftheEmperor "); })
                );
        }

        #endregion

        #region LegacyoftheWhiteTiger

        private Composite LegacyoftheWhiteTiger()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AutoBuff &&
                SpellManager.HasSpell("Legacy of the White Tiger") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentEnergy >= 10 &&
                !Me.HasAura("Legacy of the White Tiger") &&
                !Me.HasAura("Arcane Brilliance") &&
                !Me.HasAura("Dalaran Brilliance"),
                new Action(delegate { CastSpell("Legacy of the White Tiger", Me, "LegacyoftheWhiteTiger"); })
                );
        }

        #endregion

        #region LegSweepInterrupt

        public WoWUnit UnitLegSweep;

        private bool GetUnitLegSweep()
        {
            UnitLegSweep = null;

            ////using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                {
                    UnitLegSweep = (from unit in NearbyUnFriendlyPlayers
                                    where Attackable(unit, 5)
                                    where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                    where
                                        unit.IsCasting &&
                                        unit.CurrentCastTimeLeft.TotalMilliseconds <
                                        THSettings.Instance.LegSweepInterruptMs + MyLatency + 1000 &&
                                        InterruptCastNoChannel(unit) > MyLatency ||
                                        unit.IsChanneling &&
                                        InterruptCastChannel(unit) > MyLatency
                                    orderby unit.CurrentCastTimeLeft ascending
                                    select unit).FirstOrDefault();
                }
                    //PvE Search
                else if (!Me.IsInInstance || Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid)
                {
                    UnitLegSweep = (from unit in NearbyUnFriendlyUnits
                                    where Attackable(unit, 5)
                                    where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                    where !unit.IsBoss
                                    where unit.Combat
                                    where
                                        unit.IsCasting &&
                                        unit.CurrentCastTimeLeft.TotalMilliseconds <
                                        THSettings.Instance.LegSweepInterruptMs + MyLatency + 1000 &&
                                        unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency ||
                                        unit.IsChanneling &&
                                        unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency
                                    orderby unit.CurrentCastTimeLeft ascending
                                    select unit).FirstOrDefault();
                }
            }

            return UnitLegSweep != null;
        }

        private Composite LegSweepInterrupt()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.LegSweepInterrupt &&
                    _lastInterrupt + TimeSpan.FromSeconds(1) < DateTime.Now &&
                    SpellManager.HasSpell("Leg Sweep") &&
                    !CastingorGCDL() &&
                    GetSpellCooldown("Leg Sweep").TotalMilliseconds <= MyLatency &&
                    GetUnitLegSweep() &&
                    UnitLegSweep != null && UnitLegSweep.IsValid,
                    new Action(delegate
                                   {
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }

                                       while (UnitLegSweep.CurrentCastTimeLeft.TotalMilliseconds >
                                              THSettings.Instance.LegSweepInterruptMs + MyLatency)
                                       {
                                           Logging.Write("Waiting for LegSweepInterrupt");
                                       }

                                       if (UnitLegSweep.IsCasting || UnitLegSweep.IsChanneling)
                                       {
                                           CastSpell("Leg Sweep", Me, "LegSweepInterrupt");
                                           _lastInterrupt = DateTime.Now;
                                       }
                                   })));
        }

        #endregion

        #region LegSweepEnemyLowHP

        public WoWUnit UnitLegSweepEnemyLowHP;

        private bool GetUnitLegSweepEnemyLowHP()
        {
            UnitLegSweepEnemyLowHP = null;

            UnitLegSweepEnemyLowHP = (from unit in NearbyUnFriendlyPlayers
                                      where !Me.CurrentMap.IsDungeon
                                      where !Me.CurrentMap.IsRaid
                                      where Attackable(unit, 5)
                                      where !DebuffCC(unit)
                                      where !InvulnerableStun(unit)
                                      where !DebuffSilence(unit)
                                      where unit.HealthPercent <= THSettings.Instance.LegSweepEnemyHP
                                      where unit.HealthPercent > 10
                                      select unit).FirstOrDefault();

            return UnitLegSweepEnemyLowHP != null;
        }

        private Composite LegSweepEnemyLowHP()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.LegSweepEnemy &&
                    _lastInterrupt + +TimeSpan.FromSeconds(1) < DateTime.Now &&
                    SpellManager.HasSpell("Leg Sweep") &&
                    !CastingorGCDL() &&
                    GetUnitLegSweepEnemyLowHP() &&
                    UnitLegSweepEnemyLowHP != null &&
                    UnitLegSweepEnemyLowHP.IsValid &&
                    GetSpellCooldown("Leg Sweep").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(Me.CurrentTarget);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       CastSpell("Leg Sweep", Me.CurrentTarget, "LegSweepEnemyLowHP");
                                       _lastInterrupt = DateTime.Now;
                                       return RunStatus.Failure;
                                   })));
        }

        #endregion

        #region LegSweepMeLowHP

        public WoWUnit UnitLegSweepMeLowHP;

        private bool GetUnitLegSweepMeLowHP()
        {
            UnitLegSweepMeLowHP = null;

            UnitLegSweepMeLowHP = (from unit in NearbyUnFriendlyPlayers
                                   where !Me.CurrentMap.IsDungeon
                                   where !Me.CurrentMap.IsRaid
                                   where Attackable(unit, 5)
                                   where !DebuffCC(unit)
                                   where !InvulnerableStun(unit)
                                   where unit.GotTarget
                                   where !IsEnemy(unit.CurrentTarget)
                                   where unit.CurrentTarget.HealthPercent <= THSettings.Instance.LegSweepMePercent
                                   select unit).FirstOrDefault();

            if (UnitLegSweepMeLowHP == null)
            {
                UnitLegSweepMeLowHP = (from unit in NearbyUnFriendlyUnits
                                       where !unit.IsBoss
                                       where Attackable(unit, 5)
                                       where !DebuffCC(unit)
                                       where !InvulnerableStun(unit)
                                       where unit.Combat
                                       where
                                           unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                           unit.IsTargetingMyRaidMember
                                       where !IsEnemy(unit.CurrentTarget)
                                       where unit.CurrentTarget.HealthPercent <= THSettings.Instance.LegSweepMePercent
                                       select unit).FirstOrDefault();
            }
            return UnitLegSweepMeLowHP != null;
        }

        private Composite LegSweepMeLowHP()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.LegSweepMe &&
                    _lastInterrupt + +TimeSpan.FromSeconds(1) < DateTime.Now &&
                    SpellManager.HasSpell("Leg Sweep") &&
                    !CastingorGCDL() &&
                    !Me.Buffs.ContainsKey("Touch of Karma") &&
                    GetUnitLegSweepMeLowHP() &&
                    UnitLegSweepMeLowHP != null &&
                    UnitLegSweepMeLowHP.IsValid &&
                    GetSpellCooldown("Leg Sweep").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       CastSpell("Leg Sweep", UnitLegSweepMeLowHP, "LegSweepMeLowHP");
                                       _lastInterrupt = DateTime.Now;
                                       return RunStatus.Failure;
                                   })));
        }

        #endregion

        #region LifeCocoon

        private Composite LifeCocoon()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.LifeCocoon &&
                SpellManager.HasSpell("Life Cocoon") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentMana > Me.BaseMana*0.045 &&
                Healable(UnitHeal) &&
                UnitHeal.Combat &&
                UnitHeal.HealthPercent <= THSettings.Instance.LifeCocoonHP &&
                !Invulnerable(UnitHeal) &&
                !InvulnerableSpell(UnitHeal) &&
                GetSpellCooldown("Life Cocoon").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Life Cocoon", UnitHeal, "LifeCocoon");
                               })
                );
        }

        #endregion

        #region ManaTea

        private Composite ManaTea()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ManaTea &&
                SpellManager.HasSpell("Mana Tea") &&
                //LastCastSpell != "Mana Tea" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.Combat &&
                Me.HasAura("Mana Tea") &&
                Me.GetAuraByName("Mana Tea").StackCount >= 2 &&
                Me.ManaPercent <= THSettings.Instance.ManaTeaMana &&
                (!Me.IsMoving ||
                 HasGlyph.Contains("[" + 123763 + "]")) &&
                (Healable(UnitHeal) &&
                 UnitHeal.HealthPercent > THSettings.Instance.UrgentHeal ||
                 Me.ManaPercent < 20) &&
                GetSpellCooldown("Mana Tea").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Mana Tea", Me, "ManaTea");
                               })
                );
        }

        #endregion

        #region ParalysisInterrupt

        public WoWUnit UnitParalysisInterrupt;

        private bool GetUnitParalysisInterrupt()
        {
            UnitParalysisInterrupt = null;

            ////using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                {
                    UnitParalysis = (from unit in NearbyUnFriendlyPlayers
                                     where Attackable(unit, 5)
                                     where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                     where FacingOverride(unit)
                                     where
                                         unit.IsCasting &&
                                         unit.CanInterruptCurrentSpellCast &&
                                         unit.CurrentCastTimeLeft.TotalMilliseconds <
                                         THSettings.Instance.ParalysisInterruptMs + MyLatency + 1000 &&
                                         InterruptCastNoChannel(unit) > MyLatency ||
                                         unit.IsChanneling &&
                                         unit.CanInterruptCurrentSpellCast &&
                                         InterruptCastChannel(unit) > MyLatency
                                     orderby unit.CurrentCastTimeLeft ascending
                                     select unit).FirstOrDefault();
                }
                    //PvE Search
                else if (!Me.IsInInstance || Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid)
                {
                    UnitParalysisInterrupt = (from unit in NearbyUnFriendlyUnits
                                              where Attackable(unit, 5)
                                              where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                              where !unit.IsBoss
                                              where unit.Combat
                                              where FacingOverride(unit)
                                              where
                                                  unit.IsCasting &&
                                                  unit.CanInterruptCurrentSpellCast &&
                                                  unit.CurrentCastTimeLeft.TotalMilliseconds <
                                                  THSettings.Instance.ParalysisInterruptMs + MyLatency + 1000 &&
                                                  unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency ||
                                                  unit.IsChanneling &&
                                                  unit.CanInterruptCurrentSpellCast &&
                                                  unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency
                                              orderby unit.CurrentCastTimeLeft ascending
                                              select unit).FirstOrDefault();
                }
            }

            return UnitParalysisInterrupt != null;
        }

        private Composite ParalysisInterrupt()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.ParalysisInterrupt &&
                    _lastInterrupt + TimeSpan.FromSeconds(1) < DateTime.Now &&
                    SpellManager.HasSpell("Paralysis") &&
                    !CastingorGCDL() &&
                    (Me.CurrentEnergy > 15 ||
                     CurrentSpec == "MistWeaver") &&
                    GetSpellCooldown("Paralysis").TotalMilliseconds <= MyLatency &&
                    GetUnitParalysisInterrupt() &&
                    UnitParalysisInterrupt != null && UnitParalysisInterrupt.IsValid,
                    new Action(delegate
                                   {
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       SafelyFacingTarget(UnitParalysisInterrupt);

                                       while (UnitParalysisInterrupt.CurrentCastTimeLeft.TotalMilliseconds >
                                              THSettings.Instance.ParalysisInterruptMs + MyLatency)
                                       {
                                           Logging.Write("Waiting for ParalysisInterrupt");
                                       }

                                       if (UnitParalysisInterrupt.IsCasting || UnitParalysisInterrupt.IsChanneling)
                                       {
                                           CastSpell("Paralysis", UnitParalysisInterrupt, "ParalysisInterrupt");
                                           _lastInterrupt = DateTime.Now;
                                       }
                                   })));
        }

        #endregion

        #region ParalysisMeLowHP

        public WoWUnit UnitParalysis;

        private bool GetUnitParalysis()
        {
            UnitParalysis = null;
            UnitParalysis = (from unit in NearbyUnFriendlyPlayers
                             orderby unit.CurrentHealth descending
                             where unit.GotTarget
                             where !IsEnemy(unit.CurrentTarget)
                             where unit.CurrentTarget.HealthPercent <= THSettings.Instance.ParalysisMeLowHPPercent
                             where (Me.CurrentTarget != null && unit != Me.CurrentTarget || CurrentSpec == "MistWeaver")
                             where Attackable(unit, 5 + ParalysisBonusRange)
                             where !DebuffCC(unit)
                             where !DebuffDisarm(unit)
                             where !DebuffDot(unit)
                             where FacingOverride(unit)
                             where !InvulnerableSpell(unit)
                             select unit).FirstOrDefault();

            return UnitParalysis != null;
        }

        private Composite ParalysisMeLowHP()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.ParalysisMeLowHP &&
                    SpellManager.HasSpell("Paralysis") &&
                    (Me.CurrentMap.IsArena ||
                     Me.CurrentMap.IsBattleground) &&
                    !CastingorGCDL() &&
                    !Me.Buffs.ContainsKey("Touch of Karma") &&
                    (Me.CurrentEnergy > 15 ||
                     CurrentSpec == "MistWeaver") &&
                    GetSpellCooldown("Paralysis").TotalMilliseconds <= MyLatency &&
                    GetUnitParalysis() && UnitParalysis != null && UnitParalysis.IsValid,
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(Me.CurrentTarget);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       CastSpell("Paralysis", UnitParalysis, "ParalysisMeLowHP");
                                   })));
        }

        #endregion

        #region PathofBlossoms

        //private Composite PathofBlossoms()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.PathofBlossoms &&
        //        SpellManager.HasSpell("Path of Blossoms") &&
        //        !Me.Mounted &&
        //        Me.CurrentEnergy >= THSettings.Instance.PathofBlossomsEnergy &&
        //        !CastingorGCDL() &&
        //        Me.IsMoving &&
        //        !Attackable(Me.CurrentTarget, 20),
        //        new Action(delegate
        //                       {
        //                           CastSpell("Path of Blossoms", Me, "PathofBlossoms");
        //                           LastPull = DateTime.Now;
        //                       })
        //        );
        //}

        #endregion

        #region PathofBlossomsMist

        private Composite PathofBlossomsMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.PathofBlossomsMist &&
                SpellManager.HasSpell("Path of Blossoms") &&
                !Me.Mounted &&
                !Me.HasAura("Path of Blossoms") &&
                Me.ManaPercent >= THSettings.Instance.PathofBlossomsMana &&
                !CastingorGCDL() &&
                Me.IsMoving,
                new Action(delegate { CastSpell("Path of Blossoms", Me, "PathofBlossomsMist"); })
                );
        }

        #endregion

        #region PendingSpellRemove

        public void PendingSpellRemove()
        {
            if (Me.CurrentPendingCursorSpell != null &&
                !PendingSpellStopWatch.IsRunning)
            {
                PendingSpellStopWatch.Reset();
                PendingSpellStopWatch.Start();
            }

            if (Me.CurrentPendingCursorSpell != null &&
                PendingSpellStopWatch.IsRunning &&
                PendingSpellStopWatch.Elapsed.TotalMilliseconds > THSettings.Instance.PendingSpellStopWatchDuration)
            {
                Lua.DoString("RunMacroText(\"/use 4\")"); //Shirt
                PendingSpellStopWatch.Stop();
            }
        }

        #endregion

        #region PickSphere

        //public enum SphereType
        //{
        //    Chi = 3145, // created by After Life
        //    Life = 3319, // created by After Life
        //    Healing = 2866 // created by Healing Sphere spell
        //}

        public WoWObject HealingSphere;
        public DateTime LastPickSphere;

        private bool GetHealingSphere()
        {
            HealingSphere = null;

            if (!Me.Combat || !Me.CurrentMap.IsArena && !Me.CurrentMap.IsBattleground)
            {
                HealingSphere = ObjectManager.ObjectList
                                             .Where(o =>
                                                    o.Type == WoWObjectType.AreaTrigger &&
                                                    (o.Entry == 3319 || o.Entry == 2866) &&
                                                    o.Distance > 3 &&
                                                    o.Distance < THSettings.Instance.PickHealingSphereMaxDistance)
                                             .OrderBy(o => o.Distance)
                                             .FirstOrDefault();
            }
            else
            {
                HealingSphere = ObjectManager.ObjectList
                                             .Where(o =>
                                                    o.Type == WoWObjectType.AreaTrigger &&
                                                    (o.Entry == 3319 || o.Entry == 2866) &&
                                                    o.Distance > 3 &&
                                                    o.Distance < THSettings.Instance.PickHealingSphereMaxDistance &&
                                                    Me.IsSafelyFacing(o))
                                             .OrderBy(o => o.Distance)
                                             .FirstOrDefault();
            }

            return HealingSphere != null;
        }

        private Composite PickHealingSphere()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AutoMove &&
                THSettings.Instance.PickHealingSphere &&
                LastPickSphere + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                (Me.HealthPercent < THSettings.Instance.PriorityHeal ||
                 !Me.Combat &&
                 Me.HealthPercent < THSettings.Instance.DoNotHealAbove) &&
                GetHealingSphere(),
                new Action(delegate
                               {
                                   Logging.Write(Colors.Green, "PickHealingSphere");
                                   Navigator.MoveTo(HealingSphere.Location);
                                   LastPickSphere = DateTime.Now;
                                   return RunStatus.Failure;
                               })
                );
        }

        public WoWObject ChiSphere;

        private bool GetChiSphere()
        {
            ChiSphere = null;
            if (!Me.Combat || !Me.CurrentMap.IsArena && !Me.CurrentMap.IsBattleground)
            {
                ChiSphere = ObjectManager.ObjectList
                                         .Where(o =>
                                                o.Type == WoWObjectType.AreaTrigger &&
                                                o.Entry == 3145 &&
                                                o.Distance > 3 &&
                                                o.Distance < THSettings.Instance.PickChiSphereMaxDistance)
                                         .OrderBy(o => o.Distance)
                                         .FirstOrDefault();
            }
            else
            {
                ChiSphere = ObjectManager.ObjectList
                                         .Where(o =>
                                                o.Type == WoWObjectType.AreaTrigger &&
                                                o.Entry == 3145 &&
                                                o.Distance > 3 &&
                                                o.Distance < THSettings.Instance.PickChiSphereMaxDistance &&
                                                Me.IsSafelyFacing(o))
                                         .OrderBy(o => o.Distance)
                                         .FirstOrDefault();
            }
            return ChiSphere != null;
        }

        private Composite PickChiSphere()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AutoMove &&
                THSettings.Instance.PickChiSphere &&
                LastPickSphere + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                Me.CurrentChi < Me.MaxChi &&
                GetChiSphere(),
                new Action(delegate
                               {
                                   Logging.Write(Colors.Yellow, "PickChiSphere");
                                   Navigator.MoveTo(ChiSphere.Location);
                                   LastPickSphere = DateTime.Now;
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region Provoke

        private Composite Provoke()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Provoke &&
                LastPull + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Provoke") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                !IsOverrideModeOn &&
                !Me.IsInInstance &&
                //Me.CurrentTarget != null &&
                //Me.CurrentTarget.Distance < 40 &&
                Attackable(Me.CurrentTarget, 40) &&
                !Me.CurrentTarget.IsPlayer &&
                //!Me.CurrentTarget.IsWithinMeleeRange &&
                (GetDistance(Me.CurrentTarget) >= THSettings.Instance.ProvokeMinDistance ||
                 Me.CurrentTarget.Distance2D <= 5 && !Me.CurrentTarget.IsWithinMeleeRange) &&
                !Me.CurrentTarget.IsBoss &&
                (!Me.CurrentTarget.IsTargetingMeOrPet || Me.CurrentTarget.Fleeing) &&
                //FacingOverride(Me.CurrentTarget) &&
                GetSpellCooldown("Provoke").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   CastSpell("Provoke", Me.CurrentTarget, "Provoke");
                                   LastPull = DateTime.Now;
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region ProvokeBrew

        public WoWUnit UnitProvoke;

        private bool GetUnitProvoke()
        {
            UnitProvoke = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitProvoke = (from unit in NearbyUnFriendlyUnits
                               where unit.Combat
                               where Attackable(unit, 40)
                               where unit.GotTarget
                               where !unit.IsTargetingMeOrPet
                               where !unit.CurrentTarget.HasAura("Vengeance")
                               where unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember
                               select unit).FirstOrDefault();
                return UnitProvoke != null;
            }
        }

        private Composite ProvokeBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ProvokeBrew &&
                SpellManager.HasSpell("Provoke") &&
                LastCastSpell != "Leer of the Ox" &&
                LastCastSpell != "Provoke" &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                GetUnitProvoke() &&
                UnitProvoke != null &&
                UnitProvoke.IsValid &&
                GetSpellCooldown("Provoke").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   CastSpell("Provoke", UnitProvoke, "ProvokeBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region PurifiyingBrew

        private Composite PurifyingBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.PurifyingBrew &&
                SpellManager.HasSpell("Purifying Brew") &&
                Me.HasAura("Stance of the Sturdy Ox") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi > 0 &&
                (!Me.Combat &&
                 (Me.HasAura("Light Stagger") ||
                  Me.HasAura("Medium Stagger") ||
                  Me.HasAura("Heavy Stagger")) ||
                 THSettings.Instance.PurifyingBrewLevel == 1 &&
                 (Me.HasAura("Light Stagger") ||
                  Me.HasAura("Moderate Stagger") ||
                  Me.HasAura("Heavy Stagger")) ||
                 THSettings.Instance.PurifyingBrewLevel == 2 &&
                 (Me.HasAura("Moderate Stagger") ||
                  Me.HasAura("Heavy Stagger")) ||
                 THSettings.Instance.PurifyingBrewLevel == 3 &&
                 Me.HasAura("Heavy Stagger")),
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Purifying Brew", Me, "PurifyingBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region QuakingPalmMeLowHP

        public WoWUnit UnitQuakingPalm;

        private bool GetUnitQuakingPalm()
        {
            UnitQuakingPalm = null;
            UnitQuakingPalm = (from unit in NearbyUnFriendlyPlayers
                               orderby unit.CurrentHealth descending
                               where unit.GotTarget
                               where !IsEnemy(unit.CurrentTarget)
                               where unit.CurrentTarget.HealthPercent <= THSettings.Instance.ParalysisMeLowHPPercent
                               where
                                   (Me.CurrentTarget != null && unit != Me.CurrentTarget || CurrentSpec == "MistWeaver")
                               where Attackable(unit, 5)
                               where !DebuffCC(unit)
                               where !DebuffDisarm(unit)
                               where !DebuffDot(unit)
                               where FacingOverride(unit)
                               where !InvulnerableSpell(unit)
                               select unit).FirstOrDefault();

            return UnitQuakingPalm != null;
        }

        private Composite QuakingPalmMeLowHP()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.AutoRacial &&
                    SpellManager.HasSpell("Quaking Palm") &&
                    (Me.CurrentMap.IsArena ||
                     Me.CurrentMap.IsBattleground) &&
                    !CastingorGCDL() &&
                    !Me.Buffs.ContainsKey("Touch of Karma") &&
                    (Me.CurrentEnergy > 15 ||
                     CurrentSpec == "MistWeaver") &&
                    GetSpellCooldown("QuakingPalm").TotalMilliseconds <= MyLatency &&
                    GetUnitQuakingPalm() && UnitQuakingPalm != null && UnitQuakingPalm.IsValid,
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(Me.CurrentTarget);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       CastSpell("Quaking Palm", UnitQuakingPalm, "QuakingPalmMeLowHP");
                                   })));
        }

        #endregion

        #region RenewingMist

        public WoWUnit UnitRenewingMist;

        private bool GetUnitUnitRenewingMist()
        {
            UnitRenewingMist = null;
            UnitRenewingMist = (from unit in NearbyFriendlyPlayers
                                orderby MyAuraTimeLeft("Renewing Mist", unit) ascending
                                orderby unit.HealthPercent ascending
                                where Healable(unit)
                                where
                                    (Me.ManaPercent > THSettings.Instance.RenewingMistMana ||
                                     unit.HealthPercent <= THSettings.Instance.RenewingMistHP &&
                                     !MyAura("Renewing Mist", unit))
                                select unit).FirstOrDefault();
            return UnitRenewingMist != null;
        }

        public DateTime LastRenewingMist;

        private Composite RenewingMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.RenewingMist &&
                LastRenewingMist + TimeSpan.FromSeconds(7) < DateTime.Now &&
                SpellManager.HasSpell("Renewing Mist") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentMana > Me.BaseMana*0.065 &&
                (Me.IsInInstance || Me.Combat) &&
                GetUnitUnitRenewingMist() &&
                UnitRenewingMist != null &&
                UnitRenewingMist.IsValid &&
                GetSpellCooldown("Renewing Mist").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Renewing Mist", UnitRenewingMist, "RenewingMist ");
                                   LastRenewingMist = DateTime.Now;
                               })
                );
        }

        #endregion

        #region RestRotation

        public Composite RestRotation()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.UseFoodDrink &&
                !Me.IsDead &&
                !Me.IsGhost &&
                !Me.Mounted &&
                !Me.IsCasting &&
                !IsOverrideModeOn &&
                !Me.IsMoving,
                new PrioritySelector(
                    PickHealingSphere(),
                    PickChiSphere(),
                    new Decorator(
                        ret =>
                        Me.ManaPercent < THSettings.Instance.StartEating &&
                        !Me.IsSwimming &&
                        !Me.HasAura("Drink") &&
                        CurrentSpec == "MistWeaver" &&
                        Consumable.GetBestDrink(false) != null,
                        new Action(delegate
                                       {
                                           Thread.Sleep(TimeSpan.FromMilliseconds(300));

                                           if (!Me.HasAura("Drink"))
                                           {
                                               Styx.CommonBot.Rest.DrinkImmediate();
                                               StyxWoW.SleepForLagDuration();
                                           }

                                           if (Me.HasAura("Drink") &&
                                               (Me.ManaPercent <
                                                THSettings.Instance.DoNotHealAbove ||
                                                ValidUnit(UnitHeal) &&
                                                UnitHeal.HealthPercent < THSettings.Instance.UrgentHeal) &&
                                               !Me.Combat)
                                           {
                                               return RunStatus.Running;
                                           }
                                           return RunStatus.Success;
                                       }
                            )),
                    new Decorator(
                        ret =>
                        (Me.HealthPercent < 85 &&
                         (Me.CurrentMap.IsBattleground ||
                          Me.CurrentMap.IsArena) &&
                         !Me.Mounted &&
                         !Attackable(Me.CurrentTarget, 40) ||
                         Me.HealthPercent < THSettings.Instance.StartEating) &&
                        SpellManager.HasSpell("Healing Sphere") &&
                        !Me.HasAura("Drink") &&
                        !Me.HasAura("Food") &&
                        !CastingorGCDL(),
                        new Action(delegate
                                       {
                                           if (Me.CurrentEnergy < 40 && CurrentSpec != "MistWeaver")
                                           {
                                               return RunStatus.Running;
                                           }

                                           if (!IsOverrideModeOn)
                                           {
                                               DoNotMove = DateTime.Now + TimeSpan.FromSeconds(3);
                                               LastDropHealingSphere = DateTime.Now;
                                               CastSpell("Healing Sphere", Me, "RestRotation");
                                               while (LastDropHealingSphere + TimeSpan.FromMilliseconds(300) <
                                                      DateTime.Now &&
                                                      Me.CurrentPendingCursorSpell == null)
                                               {
                                                   Logging.Write("Waiting for RestRotation CurrentPendingCursorSpell");
                                               }
                                               SpellManager.ClickRemoteLocation(CalculateDropSphereLocation(Me));
                                               if (DoNotMove < DateTime.Now)
                                               {
                                                   return RunStatus.Running;
                                               }
                                               return RunStatus.Success;
                                           }
                                           return RunStatus.Success;
                                       }
                            )),
                    new Decorator(
                        ret =>
                        Me.HealthPercent < THSettings.Instance.StartEating &&
                        !Me.IsSwimming &&
                        !Me.HasAura("Food") &&
                        !SpellManager.HasSpell("Healing Sphere") &&
                        Consumable.GetBestFood(false) != null,
                        new Action(delegate
                                       {
                                           Thread.Sleep(TimeSpan.FromMilliseconds(300));

                                           if (!Me.HasAura("Food"))
                                           {
                                               Styx.CommonBot.Rest.FeedImmediate();
                                               StyxWoW.SleepForLagDuration();
                                           }

                                           if (Me.HasAura("Food") &&
                                               Me.HealthPercent <
                                               THSettings.Instance.DoNotHealAbove &&
                                               !Me.Combat)
                                           {
                                               return RunStatus.Running;
                                           }
                                           return RunStatus.Success;
                                       }
                            ))
                    ));
        }

        #endregion

        #region Revival

        private Composite Revival()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Revival &&
                SpellManager.HasSpell("Revival") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentMana > Me.BaseMana*0.07 &&
                CountFriendlyNearNeedHeal(Me, 40, THSettings.Instance.RevivalHP) >=
                THSettings.Instance.RevivalUnits &&
                GetSpellCooldown("Revival").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Revival", Me, "Revival");
                               })
                );
        }

        #endregion

        #region Rising Sun Kick

        private Composite RisingSunKick()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.RisingSunKick &&
                SpellManager.HasSpell("Rising Sun Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 5) &&
                (Me.CurrentChi >= 2 &&
                 !Me.CurrentTarget.IsPlayer ||
                 Me.CurrentChi >= 3) &&
                FacingOverride(Me.CurrentTarget) &&
                GetSpellCooldown("Rising Sun Kick").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   CastSpell("Rising Sun Kick", Me.CurrentTarget, "RisingSunKick");
                                   //Logging.Write("HoverHeight: " + Me.CurrentTarget.HoverHeight);
                               })
                );
        }

        #endregion

        #region Roll

        //public DateTime ClickToMove;

        private Composite Roll()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Roll &&
                LastPull + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Roll") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 40) &&
                !Me.CurrentTarget.IsFlying &&
                !Me.CurrentTarget.IsPet &&
                Me.IsSafelyFacing(Me.CurrentTarget, 10) &&
                !SpecialUnit.Contains(Me.CurrentTarget.Entry) &&
                Me.CurrentTarget.Distance - Me.CurrentTarget.Distance2D < 5 &&
                GetDistance(Me.CurrentTarget) >= THSettings.Instance.RollMinDistance &&
                GetDistance(Me.CurrentTarget) <= THSettings.Instance.RollMaxDistance &&
                !DebuffRoot(Me) &&
                GetSpellCooldown("Roll").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   InputLasting = DateTime.Now + TimeSpan.FromMilliseconds(500);
                                   InputBlockUse(true, "Roll");
                                   TrueFacingDelay = DateTime.Now;
                                   Me.CurrentTarget.Face();
                                   if (!IsOverrideModeOn && !Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       while (THSettings.Instance.AutoFace &&
                                              TrueFacingDelay +
                                              TimeSpan.FromMilliseconds(THSettings.Instance.TrueFacingTimeOut) >
                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                       {
                                           Me.CurrentTarget.Face();
                                           //Logging.Write("Roll - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       CastSpell("Roll", Me.CurrentTarget, "Roll");
                                       return RunStatus.Failure;
                                   }

                                   LastPull = DateTime.Now;

                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region RollBrew

        //public DateTime ClickToMove;

        private Composite RollBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.RollBrew &&
                LastPull + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Roll") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 40) &&
                !Me.CurrentTarget.IsFlying &&
                !Me.CurrentTarget.IsPet &&
                Me.IsSafelyFacing(Me.CurrentTarget, 10) &&
                !SpecialUnit.Contains(Me.CurrentTarget.Entry) &&
                Me.CurrentTarget.Distance - Me.CurrentTarget.Distance2D < 5 &&
                GetDistance(Me.CurrentTarget) >= THSettings.Instance.RollBrewMinDistance &&
                GetDistance(Me.CurrentTarget) <= THSettings.Instance.RollBrewMaxDistance &&
                !DebuffRoot(Me) &&
                GetSpellCooldown("Roll").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   InputLasting = DateTime.Now + TimeSpan.FromMilliseconds(500);
                                   InputBlockUse(true, "Roll");
                                   TrueFacingDelay = DateTime.Now;
                                   Me.CurrentTarget.Face();
                                   if (!IsOverrideModeOn && !Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       while (THSettings.Instance.AutoFace &&
                                              TrueFacingDelay +
                                              TimeSpan.FromMilliseconds(THSettings.Instance.TrueFacingTimeOut) >
                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                       {
                                           Me.CurrentTarget.Face();
                                           //Logging.Write("Roll - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       CastSpell("Roll", Me.CurrentTarget, "RollBrew");
                                       return RunStatus.Failure;
                                   }

                                   LastPull = DateTime.Now;

                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region RushingJadeWind

        private Composite RushingJadeWind()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.RushingJadeWind &&
                THSettings.Instance.AoE &&
                SpellManager.HasSpell("Rushing Jade Wind") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                Attackable(Me.CurrentTarget, 10) &&
                FacingOverride(Me.CurrentTarget) &&
                CountEneyNeary(Me, 10) >= THSettings.Instance.RushingJadeWindNumberUnit &&
                GetSpellCooldown("Rushing Jade Wind").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   TrueFacingDelay = DateTime.Now;
                                   ConstantFace(Me.CurrentTarget);
                                   if (!Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                   {
                                       while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                              TimeSpan.FromMilliseconds(
                                                                                  THSettings.Instance.TrueFacingTimeOut) >
                                                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                       {
                                           Me.CurrentTarget.Face();
                                           //Logging.Write("RushingJadeWind - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                   {
                                       CastSpell("Rushing Jade Wind", Me, "RushingJadeWind");
                                   }
                               })
                );
        }

        #endregion

        #region RushingJadeWindBrew

        private Composite RushingJadeWindBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.RushingJadeWind &&
                THSettings.Instance.AoE &&
                SpellManager.HasSpell("Rushing Jade Wind") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                Attackable(Me.CurrentTarget, 10) &&
                FacingOverride(Me.CurrentTarget) &&
                (CountEneyNearyMeFacing(10) >= THSettings.Instance.RushingJadeWindNumberUnit ||
                 MyAuraTimeLeft("Shuffle", Me) < 3000 && !Attackable(Me.CurrentTarget, 6)) &&
                GetSpellCooldown("Rushing Jade Wind").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   TrueFacingDelay = DateTime.Now;
                                   ConstantFace(Me.CurrentTarget);
                                   if (!Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                   {
                                       while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                              TimeSpan.FromMilliseconds(
                                                                                  THSettings.Instance.TrueFacingTimeOut) >
                                                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                       {
                                           Me.CurrentTarget.Face();
                                           //Logging.Write("ChiBurst - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(Me.CurrentTarget, 15))
                                   {
                                       CastSpell("Rushing Jade Wind", Me, "RushingJadeWindBrew");
                                   }
                               })
                );
        }

        #endregion

        #region RushingJadeWindMist

        private Composite RushingJadeWindMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.RushingJadeWindMist &&
                SpellManager.HasSpell("Rushing Jade Wind") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                CountFriendlyNearNeedHeal(Me, 10, THSettings.Instance.RushingJadeWindMistHP) >=
                THSettings.Instance.RushingJadeWindMistNumberUnit,
                new Action(delegate
                               {
                                   CastSpell("Rushing Jade Wind", Me, "RushingJadeWindMist");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region SoothingMist

        public WoWUnit SoothingMistCurrentTarget;

        private Composite SoothingMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SoothingMist &&
                SpellManager.HasSpell("Soothing Mist") &&
                !Me.Mounted &&
                !Me.IsMoving &&
                !CastingorGCDL() &&
                Me.CurrentMana > Me.BaseMana*0.095 &&
                Healable(UnitHeal) &&
                (!Me.IsChanneling ||
                 Me.IsChanneling &&
                 Me.ChanneledCastingSpellId != 115175 ||
                 Me.IsChanneling &&
                 Me.ChanneledCastingSpellId == 115175 &&
                 UnitHeal != SoothingMistCurrentTarget &&
                 SoothingMistCurrentTarget != null &&
                 UnitHeal.HealthPercent > SoothingMistCurrentTarget.HealthPercent - 5) &&
                UnitHeal.HealthPercent <= THSettings.Instance.SoothingMistHP,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Soothing Mist", UnitHeal, "SoothingMist");
                                   SoothingMistCurrentTarget = UnitHeal;
                               })
                );
        }

        #endregion

        #region SpearHandStrike

        public WoWUnit UnitSpearHandStrike;

        private bool GetUnitSpearHandStrike()
        {
            UnitSpearHandStrike = null;

            ////using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                {
                    UnitSpearHandStrike = (from unit in NearbyUnFriendlyPlayers
                                           where Attackable(unit, 5)
                                           where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                           where FacingOverride(unit)
                                           where
                                               unit.IsCasting &&
                                               unit.CanInterruptCurrentSpellCast &&
                                               unit.CurrentCastTimeLeft.TotalMilliseconds <
                                               THSettings.Instance.SpearHandStrikeMs + MyLatency + 1000 &&
                                               InterruptCastNoChannel(unit) > MyLatency ||
                                               unit.IsChanneling &&
                                               unit.CanInterruptCurrentSpellCast &&
                                               InterruptCastChannel(unit) > MyLatency
                                           orderby unit.CurrentCastTimeLeft ascending
                                           select unit).FirstOrDefault();
                }
                    //PvE Search
                else if (!Me.IsInInstance || Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid)
                {
                    UnitSpearHandStrike = (from unit in NearbyUnFriendlyUnits
                                           where Attackable(unit, 5)
                                           where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                           where !unit.IsBoss
                                           where unit.Combat
                                           where FacingOverride(unit)
                                           where
                                               unit.IsCasting &&
                                               unit.CanInterruptCurrentSpellCast &&
                                               unit.CurrentCastTimeLeft.TotalMilliseconds <
                                               THSettings.Instance.SpearHandStrikeMs + MyLatency + 1000 &&
                                               unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency ||
                                               unit.IsChanneling &&
                                               unit.CanInterruptCurrentSpellCast &&
                                               unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency
                                           orderby unit.CurrentCastTimeLeft ascending
                                           select unit).FirstOrDefault();
                }
            }

            return UnitSpearHandStrike != null;
        }

        private Composite SpearHandStrike()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.SpearHandStrike &&
                    _lastInterrupt + TimeSpan.FromSeconds(1) < DateTime.Now &&
                    SpellManager.HasSpell("Spear Hand Strike") &&
                    GetSpellCooldown("Spear Hand Strike").TotalMilliseconds <= MyLatency &&
                    GetUnitSpearHandStrike() &&
                    UnitSpearHandStrike != null && UnitSpearHandStrike.IsValid,
                    new Action(delegate
                                   {
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       SafelyFacingTarget(UnitSpearHandStrike);

                                       while (UnitSpearHandStrike.CurrentCastTimeLeft.TotalMilliseconds >
                                              THSettings.Instance.SpearHandStrikeMs + MyLatency)
                                       {
                                           Logging.Write("Waiting for SpearHandStrike");
                                       }

                                       if (UnitSpearHandStrike.IsCasting || UnitSpearHandStrike.IsChanneling)
                                       {
                                           CastSpell("Spear Hand Strike", UnitSpearHandStrike, "SpearHandStrike");
                                           _lastInterrupt = DateTime.Now;
                                       }
                                       return RunStatus.Failure;
                                   })));
        }

        #endregion

        #region SpearHandStrikePlayerLowHP

        private Composite SpearHandStrikePlayerLowHP()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.SpearHandStrikeSilence &&
                    _lastInterrupt + TimeSpan.FromSeconds(1) < DateTime.Now &&
                    SpellManager.HasSpell("Spear Hand Strike") &&
                    Attackable(Me.CurrentTarget, 5) &&
                    Me.CurrentTarget.IsPlayer &&
                    Me.CurrentTarget.PowerType == WoWPowerType.Mana &&
                    Me.CurrentTarget.IsFacing(Me) &&
                    !DebuffCC(Me.CurrentTarget) &&
                    !DebuffSilence(Me.CurrentTarget) &&
                    Me.CurrentTarget.HealthPercent <= THSettings.Instance.SpearHandStrikeSilenceHP &&
                    GetSpellCooldown("Spear Hand Strike").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       SafelyFacingTarget(Me.CurrentTarget);
                                       if (Me.IsCasting || Me.IsChanneling)
                                       {
                                           SpellManager.StopCasting();
                                       }
                                       CastSpell("Spear Hand Strike", Me.CurrentTarget, "SpearHandStrikePlayerLowHP");
                                       _lastInterrupt = DateTime.Now;
                                       return RunStatus.Failure;
                                   })));
        }

        #endregion

        #region SpinningCraneKick

        private Composite SpinningCraneKick()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SpinningCraneKick &&
                THSettings.Instance.AoE &&
                SpellManager.HasSpell("Spinning Crane Kick") &&
                !Me.Mounted &&
                !Me.CurrentMap.IsBattleground &&
                !Me.CurrentMap.IsArena &&
                !CastingorGCDL() &&
                Me.HasAura("Tiger Power") &&
                Me.CurrentEnergy >= 30 &&
                //Me.CurrentChi < Me.MaxChi &&
                CountEneyNeary(Me, 10) >= THSettings.Instance.SpinningCraneKickNumberUnit,
                new Action(delegate { CastSpell("Spinning Crane Kick", Me, "SpinningCraneKick"); })
                );
        }

        #endregion

        #region SpinningCraneKickBrew

        private Composite SpinningCraneKickBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SpinningCraneKickBrew &&
                THSettings.Instance.AoE &&
                SpellManager.HasSpell("Spinning Crane Kick") &&
                !Me.Mounted &&
                !Me.CurrentMap.IsBattleground &&
                !Me.CurrentMap.IsArena &&
                !CastingorGCDL() &&
                Me.HasAura("Tiger Power") &&
                Me.CurrentEnergy >= 30 &&
                //Me.CurrentChi < Me.MaxChi &&
                CountEneyNeary(Me, 10) >= THSettings.Instance.SpinningCraneKickBrewNumberUnit,
                new Action(delegate { CastSpell("Spinning Crane Kick", Me, "SpinningCraneKickBrew"); })
                );
        }

        #endregion

        #region SpinningCraneKickMist

        private Composite SpinningCraneKickMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SpinningCraneKickMist &&
                SpellManager.HasSpell("Spinning Crane Kick") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentMana > Me.BaseMana*0.055 &&
                CountFriendlyNearNeedHeal(Me, 10, THSettings.Instance.SpinningCraneKickMistHP) >=
                THSettings.Instance.SpinningCraneKickMistUnits,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Spinning Crane Kick", Me, "SpinningCraneKickMist");
                               })
                );
        }

        #endregion

        #region SpinningFireBlossom

        public DateTime LastSpinningFireBlossom;

        private Composite SpinningFireBlossom()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SpinningFireBlossom &&
                SpellManager.HasSpell("Spinning Fire Blossom") &&
                LastSpinningFireBlossom + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                !Me.Mounted &&
                Me.CurrentChi > 0 &&
                Attackable(Me.CurrentTarget, 50) &&
                !InvulnerableRootandSnare(Me.CurrentTarget) &&
                Me.CurrentTarget.IsPlayer &&
                Me.CurrentTarget.IsMoving &&
                !Me.CurrentTarget.IsFacing(Me) &&
                Me.CurrentTarget.Distance > THSettings.Instance.SpinningFireBlossomMinDistance &&
                (Me.IsSafelyFacing(Me.CurrentTarget, 10) && !HasGlyph.Contains("123405")) &&
                !CastingorGCDL(),
                new Action(delegate
                               {
                                   TrueFacingDelay = DateTime.Now;
                                   ConstantFace(Me.CurrentTarget);
                                   if (!Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       while (THSettings.Instance.AutoFace && TrueFacingDelay +
                                                                              TimeSpan.FromMilliseconds(
                                                                                  THSettings.Instance.TrueFacingTimeOut) >
                                                                              DateTime.Now &&
                                              !Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                       {
                                           Me.CurrentTarget.Face();
                                           //Logging.Write("SpinningFireBlossom - Waiting TrueFacing");
                                       }
                                   }
                                   if (Me.IsSafelyFacing(Me.CurrentTarget, 10))
                                   {
                                       CastSpell("Spinning Fire Blossom", Me, "SpinningFireBlossom");
                                   }
                                   LastSpinningFireBlossom = DateTime.Now;
                               })
                );
        }

        #endregion

        #region SummonBlackOxStatue

        public WoWPoint SummonBlackOxStatueLastPosition;
        public DateTime LastDropSummonBlackOxStatue;
        public WoWUnit MyBlackOxStatue;

        private bool GetMyMyBlackOxStatueStatue()
        {
            MyBlackOxStatue = null;

            MyBlackOxStatue =
                ObjectManager.GetObjectsOfType<WoWUnit>(true, false)
                             .FirstOrDefault(
                                 p =>
                                 p.CreatedByUnitGuid == Me.Guid && p.Name.Contains("Statue") &&
                                 p.Distance <= THSettings.Instance.SummonBlackOxStatueDistance);

            return MyBlackOxStatue != null;
        }

        private Composite SummonBlackOxStatue()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SummonBlackOxStatue &&
                LastDropSummonBlackOxStatue + TimeSpan.FromMilliseconds(5000) < DateTime.Now &&
                SpellManager.HasSpell("Summon Black Ox Statue") &&
                (Me.Combat || Me.IsInInstance) &&
                Attackable(Me.CurrentTarget, 20) &&
                !CastingorGCDL() &&
                !GetMyMyBlackOxStatueStatue(),
                new Action(delegate
                               {
                                   LastDropSummonBlackOxStatue = DateTime.Now;
                                   CastSpell("Summon Black Ox Statue", Me, "SummonBlackOxStatue");
                                   while (LastDropSummonBlackOxStatue + TimeSpan.FromMilliseconds(300) < DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for Summon Black Ox Statue CurrentPendingCursorSpell");
                                   }
                                   SummonBlackOxStatueLastPosition = WoWMathHelper.CalculatePointBehind(Me.Location,
                                                                                                        Me.Rotation, 3);
                                   SpellManager.ClickRemoteLocation(SummonBlackOxStatueLastPosition);
                               }
                    ));
        }

        #endregion

        #region SummonJadeSerpentStatue

        public WoWPoint SummonJadeSerpentStatueLastPosition;
        public DateTime LastDropSummonJadeSerpentStatue;
        public WoWUnit MyJadeSerpentStatue;

        private bool GetMyJadeSerpentStatue()
        {
            MyJadeSerpentStatue = null;

            MyJadeSerpentStatue =
                ObjectManager.GetObjectsOfType<WoWUnit>(true, false)
                             .FirstOrDefault(
                                 p =>
                                 p.CreatedByUnitGuid == Me.Guid && p.Name.Contains("Statue") &&
                                 p.Distance <= THSettings.Instance.SummonJadeSerpentStatueDistance);

            return MyJadeSerpentStatue != null;
        }

        private Composite SummonJadeSerpentStatue()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SummonJadeSerpentStatue &&
                LastDropSummonJadeSerpentStatue + TimeSpan.FromMilliseconds(5000) < DateTime.Now &&
                SpellManager.HasSpell("Summon Jade Serpent Statue") &&
                (Me.Combat || Me.IsInInstance) &&
                Me.CurrentMana > Me.BaseMana*0.09 &&
                !CastingorGCDL() &&
                CountEneyNearyWorthDropStatue(40) > 0 &&
                !GetMyJadeSerpentStatue(),
                new Action(delegate
                               {
                                   LastDropSummonJadeSerpentStatue = DateTime.Now;
                                   CastSpell("Summon Jade Serpent Statue", Me, "SummonJadeSerpentStatue");
                                   while (LastDropSummonJadeSerpentStatue + TimeSpan.FromMilliseconds(300) <
                                          DateTime.Now &&
                                          Me.CurrentPendingCursorSpell == null)
                                   {
                                       Logging.Write("Waiting for Summon Black Ox Statue CurrentPendingCursorSpell");
                                   }
                                   SummonJadeSerpentStatueLastPosition = WoWMathHelper.CalculatePointBehind(Me.Location,
                                                                                                            Me.Rotation,
                                                                                                            -10);
                                   SpellManager.ClickRemoteLocation(SummonJadeSerpentStatueLastPosition);
                               }
                    ));
        }

        #endregion

        #region StopCastingCheck

        public void StopCastingCheck()
        {
            //if (Me.GotTarget && Me.CurrentTarget.Entry == 62442 &&//TsulongTsulong
            //    (!Me.CurrentTarget.Attackable || Me.CurrentTarget.IsFriendly))
            //    return;

            if (Me.IsCasting && Me.IsChanneling && Me.ChanneledCastingSpellId == 115175 &&
                SoothingMistCurrentTarget != null &&
                SoothingMistCurrentTarget.HealthPercent > THSettings.Instance.DoNotHealAbove)
            {
                SpellManager.StopCasting();
                Logging.Write(DateTime.Now.ToString("ss:fff ") + "Stop Casting: Soothing Mist, full HP");
            }

            //116694 Surging Mist //Enveloping Mist 124682
            if ( //(Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena) &&
                Me.IsCasting && (Me.CastingSpellId == 116694 || Me.CastingSpellId == 124682))
            {
                SpellManager.StopCasting();
                Logging.Write(DateTime.Now.ToString("ss:fff ") +
                              "Stop Casting: Don't Hard Cast Surging Mist and Enveloping Mist");
            }
            //115294 Mana Tea No Glyph
            if (Me.IsChanneling && Me.ChanneledCastingSpellId == 115294 && Me.ManaPercent > 90)
            {
                SpellManager.StopCasting();
                Logging.Write(DateTime.Now.ToString("ss:fff ") + "Stop Casting: Full Mana");
            }

            //117952  [Crackling Jade Lightning]
            if (Me.IsChanneling && Me.ChanneledCastingSpellId == 117952 && Me.CurrentChi == Me.MaxChi)
            {
                SpellManager.StopCasting();
                Logging.Write(DateTime.Now.ToString("ss:fff ") + "Stop Casting: Full Chi");
            }

            if (CurrentSpec == "MistWeaver" && Me.IsChanneling && Me.ChanneledCastingSpellId == 117952 &&
                UnitHeal != null && UnitHeal.IsValid && UnitHeal.HealthPercent < THSettings.Instance.PriorityHeal)
            {
                SpellManager.StopCasting();
                Logging.Write(DateTime.Now.ToString("ss:fff ") + "Stop Casting: Need to heal instead of DPS");
            }
        }

        #endregion

        #region SurgingMist

        private Composite SurgingMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SurgingMist &&
                SpellManager.HasSpell("Surging Mist") &&
                !Me.Mounted &&
                !Me.IsMoving &&
                !CastingorGCDL() &&
                Me.CurrentMana > Me.BaseMana*0.08 &&
                Me.CurrentChi < Me.MaxChi &&
                Healable(UnitHeal) &&
                UnitHeal.Combat &&
                UnitHeal.HealthPercent <= THSettings.Instance.SurgingMistHP,
                new Action(delegate
                               {
                                   //Logging.Write("Enter SurgingMist");
                                   if (!Me.IsChanneling ||
                                       Me.IsChanneling &&
                                       Me.ChanneledCastingSpellId != 115175 || //Soothing Mist
                                       Me.IsChanneling &&
                                       Me.ChanneledCastingSpellId == 115175 &&
                                       (SoothingMistCurrentTarget != UnitHeal ||
                                        HasGlyph.Contains("120483"))) //Glyph of Surging Mist
                                   {
                                       //Logging.Write("Soothing Mist before SurgingMist");
                                       CastSpell("Soothing Mist", UnitHeal, "Soothing Mist before Surging Mist");
                                       SoothingMistCurrentTarget = UnitHeal;
                                   }
                                   else
                                   {
                                       if (THSettings.Instance.ThunderFocusTeaSurgingMist &&
                                           UnitHeal.HealthPercent <= THSettings.Instance.ThunderFocusTeaSurgingMistHP &&
                                           GetSpellCooldown("Thunder Focus Tea").TotalMilliseconds <= MyLatency)
                                       {
                                           //Logging.Write("Thunder Focus Tea before SurgingMist");
                                           CastSpell("Thunder Focus Tea", Me, "ThunderFocusTeaSurgingMistHP");
                                       }
                                       Logging.Write("Now SurgingMist");
                                       CastSpell("Surging Mist", UnitHeal, "SurgingMist");
                                   }
                                   //SafelyFacingTarget(Me.CurrentTarget);
                               })
                );
        }

        #endregion

        #region SurgingMistVistaMist

        //Me.GetAuraByName("Vital Mists").StackCount < 5
        private Composite SurgingMistVistaMist()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SurgingMist &&
                SpellManager.HasSpell("Surging Mist") &&
                !Me.Mounted &&
                Me.HasAura("Vital Mist") &&
                (!Me.IsMoving || Me.GetAuraByName("Vital Mists").StackCount == 5) &&
                Me.GetAuraByName("Vital Mists").StackCount >= THSettings.Instance.SurgingMistVistaMistStack &&
                !CastingorGCDL() &&
                //Me.CurrentMana > Me.BaseMana * 0.08 &&
                //Me.CurrentChi < Me.MaxChi &&
                Healable(UnitHeal) &&
                UnitHeal.Combat &&
                (UnitHeal.HealthPercent <= THSettings.Instance.SurgingMistHP ||
                 Me.GetAuraByName("Vital Mists").TimeLeft.TotalMilliseconds <= 3000),
                new Action(delegate
                               {
                                   //Logging.Write("Enter SurgingMist");
                                   if (Me.GetAuraByName("Vital Mists").StackCount < 5 &&
                                       (!Me.IsChanneling ||
                                        Me.IsChanneling &&
                                        Me.ChanneledCastingSpellId != 115175 || //Soothing Mist
                                        Me.IsChanneling &&
                                        Me.ChanneledCastingSpellId == 115175 &&
                                        (SoothingMistCurrentTarget != UnitHeal ||
                                         HasGlyph.Contains("120483")))) //Glyph of Surging Mist
                                   {
                                       //Logging.Write("Soothing Mist before SurgingMist");
                                       CastSpell("Soothing Mist", UnitHeal, "Soothing Mist before Surging Mist");
                                       SoothingMistCurrentTarget = UnitHeal;
                                   }
                                   else
                                   {
                                       if (THSettings.Instance.ThunderFocusTeaSurgingMist &&
                                           UnitHeal.HealthPercent <= THSettings.Instance.ThunderFocusTeaSurgingMistHP &&
                                           GetSpellCooldown("Thunder Focus Tea").TotalMilliseconds <= MyLatency)
                                       {
                                           //Logging.Write("Thunder Focus Tea before SurgingMist");
                                           CastSpell("Thunder Focus Tea", Me, "ThunderFocusTeaSurgingMistHP");
                                       }
                                       Logging.Write("Now SurgingMist");
                                       CastSpell("Surging Mist", UnitHeal, "SurgingMistVistaMist");
                                   }
                                   //SafelyFacingTarget(Me.CurrentTarget);
                               })
                );
        }

        #endregion

        #region Transcendence

        public WoWObject TranscendenceImage;

        private bool GetTranscendence()
        {
            TranscendenceImage = null;

            TranscendenceImage = ObjectManager.ObjectList
                                              .Where(o =>
                                                     o.Entry == 54569 &&
                                                     //o.Type == WoWObjectType.Unit &&
                                                     o.Name == Me.Name &&
                                                     (o.Distance < THSettings.Instance.TranscendenceDistance ||
                                                      o.Distance < 10 && !o.InLineOfSight))
                                              .OrderBy(o => o.Distance).FirstOrDefault();

            //if (TranscendenceImage != null)
            //{
            //    foreach (WoWObject objectaround in TranscendenceImage)
            //    {
            //        Logging.Write("Entry: " + objectaround.Entry + "Type: " + objectaround.Type + " Name: " +
            //                      objectaround.Name + " Distance: " + objectaround.Distance);
            //    }
            //}

            //if (TranscendenceImage != null)
            //{
            //    Logging.Write("Found My TranscendenceImage");
            //}

            return TranscendenceImage != null;
        }

        private Composite Transcendence()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Transcendence &&
                SpellManager.HasSpell("Transcendence: Transfer") &&
                !Me.Mounted &&
                (Me.CurrentMap.IsArena ||
                 Me.CurrentMap.IsBattleground) &&
                !CastingorGCDL() &&
                Me.HealthPercent <= THSettings.Instance.TranscendenceHP &&
                (Me.CurrentEnergy > 15 ||
                 CurrentSpec == "MistWeaver") &&
                (!Attackable(Me.CurrentTarget, 8) ||
                 Attackable(Me.CurrentTarget, 7) &&
                 Me.CurrentTarget.HealthPercent > Me.HealthPercent) &&
                GetSpellCooldown("Transcendence: Transfer").TotalMilliseconds <= MyLatency &&
                GetTranscendence() &&
                TranscendenceImage.Distance <= 40 + TranscendenceRangeBonus &&
                (TranscendenceImage.Distance > THSettings.Instance.TranscendenceDistance ||
                 TranscendenceImage.Distance > 10 && !TranscendenceImage.InLineOfSight),
                new Action(delegate
                               {
                                   if (Me.IsMoving)
                                   {
                                       WoWMovement.MoveStop();
                                   }
                                   CastSpell("Transcendence: Transfer", Me, "Transcendence");
                                   LastTigerPowerProc = DateTime.Now;
                               })
                );
        }

        #endregion

        #region TigerPalmMist

        private Composite TigerPalmMistTigerPower()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigerPalmMist &&
                SpellManager.HasSpell("Tiger Palm") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.ManaPercent >= THSettings.Instance.TigerPalmMistMana &&
                Me.CurrentChi >= 1 &&
                THSettings.Instance.TigerPalmMistTigerPower &&
                !Me.HasAura("Tiger Power") &&
                GetUnitMeleeMist(),
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitMeleeMist);

                                   CastSpell("Tiger Palm", UnitMeleeMist, "TigerPalmMistTigerPower");
                               })
                );
        }

        private Composite TigerPalmVitalMists()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigerPalmMist &&
                SpellManager.HasSpell("Tiger Palm") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 1 &&
                Me.ManaPercent >= THSettings.Instance.TigerPalmMistMana &&
                (THSettings.Instance.TigerPalmMistVitalMists &&
                 !Me.HasAura("Vital Mists") ||
                 THSettings.Instance.TigerPalmMistVitalMists &&
                 Me.HasAura("Vital Mists") &&
                 Me.GetAuraByName("Vital Mists").StackCount < 5) &&
                GetUnitMeleeMist(),
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitMeleeMist);

                                   CastSpell("Tiger Palm", UnitMeleeMist, "TigerPalmVitalMists");
                               })
                );
        }

        #endregion

        #region  TigersLustMe

        private Composite TigersLustMe()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigersLust &&
                THSettings.Instance.TigersLustMe &&
                SpellManager.HasSpell("Tiger's Lust") &&
                !Me.Mounted &&
                Me.CurrentChi > 0 &&
                !CastingorGCDL() &&
                Attackable(Me.CurrentTarget, 30) &&
                DebuffRootorSnare(Me) &&
                !Me.CurrentTarget.IsWithinMeleeRange &&
                FacingOverride(Me.CurrentTarget) &&
                GetSpellCooldown("Tiger's Lust").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   Logging.Write("Tiger's Lust: " + Me.MovementInfo.RunSpeed);

                                   CastSpell("Tiger's Lust", Me, "TigersLustMe");
                                   //Logging.Write("HoverHeight: " + Me.CurrentTarget.HoverHeight);
                               })
                )
                ;
        }

        #endregion

        #region  TigersLustFriend

        public WoWUnit UnitTigersLust;

        private bool GetUnitTigersLust()
        {
            UnitTigersLust = null;

            if (UnitTigersLust == null && THSettings.Instance.TigersLustMelee)
            {
                //using (StyxWoW.Memory.AcquireFrame())
                {
                    UnitTigersLust = (from unit in NearbyFriendlyPlayers
                                      where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                      where GetDistance(unit) < 20
                                      where unit.HealthPercent > 30
                                      where !DebuffCC(unit)
                                      where TalentSort(unit) == 1
                                      where unit.Class != WoWClass.Paladin
                                      where unit.Class != WoWClass.Druid
                                      where unit.Class != WoWClass.Shaman
                                      where unit.GotTarget
                                      where IsEnemy(unit.CurrentTarget)
                                      where unit.Location.Distance(unit.CurrentTarget.Location) > 6
                                      where DebuffRootorSnare(unit)
                                      where Healable(unit)
                                      select unit).FirstOrDefault();
                }
            }

            if (UnitTigersLust == null && THSettings.Instance.TigersLustHealer)
            {
                //using (StyxWoW.Memory.AcquireFrame())
                {
                    UnitTigersLust = (from unit in NearbyFriendlyPlayers
                                      where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                      where GetDistance(unit) < 20
                                      where unit.HealthPercent > 10
                                      where !DebuffCC(unit)
                                      where TalentSort(unit) == 4
                                      where unit.Class != WoWClass.Paladin && unit.Class != WoWClass.Druid
                                      where CountMeleeDPSTarget(unit) > 0
                                      where DebuffRootorSnare(unit)
                                      where Healable(unit)
                                      select unit).FirstOrDefault();
                }
            }

            if (UnitTigersLust == null && THSettings.Instance.TigersLustCaster)
            {
                //using (StyxWoW.Memory.AcquireFrame())
                {
                    UnitTigersLust = (from unit in NearbyFriendlyPlayers
                                      where !Blacklist.Contains(unit.Guid, BlacklistFlags.All)
                                      where GetDistance(unit) < 20
                                      where unit.HealthPercent > 30
                                      where !DebuffCC(unit)
                                      where TalentSort(unit) > 1
                                      where TalentSort(unit) < 4
                                      where CountMeleeDPSTarget(unit) > 0
                                      where DebuffRootorSnare(unit)
                                      where Healable(unit)
                                      select unit).FirstOrDefault();
                }
            }


            return UnitTigersLust != null;
        }

        private Composite TigersLustFriend()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigersLust &&
                (THSettings.Instance.TigersLustMelee ||
                 THSettings.Instance.TigersLustCaster ||
                 THSettings.Instance.TigersLustHealer) &&
                SpellManager.HasSpell("Tiger's Lust") &&
                !Me.Mounted &&
                Me.CurrentChi > 0 &&
                !CastingorGCDL() &&
                GetSpellCooldown("Tiger's Lust").TotalMilliseconds <= MyLatency &&
                GetUnitTigersLust(),
                new Action(delegate
                               {
                                   //Logging.Write("Tiger's Lust: " + Me.MovementInfo.RunSpeed);

                                   CastSpell("Tiger's Lust", UnitTigersLust, "TigersLustFriend");
                                   //Logging.Write("HoverHeight: " + Me.CurrentTarget.HoverHeight);
                               })
                )
                ;
        }

        #endregion

        #region TigerPowerBuff

        public DateTime LastTigerPowerBuff;

        private Composite TigerPowerBuff()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigerPalm &&
                SpellManager.HasSpell("Tiger Palm") &&
                LastTigerPowerBuff + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                (Me.CurrentChi >= 1 ||
                 Me.HasAura("Combo Breaker: Tiger Palm") ||
                 SpellManager.HasSpell("Brewmaster Training")) &&
                (!Me.HasAura("Tiger Power") ||
                 Me.GetAuraByName("Tiger Power").TimeLeft.TotalMilliseconds <= 2000 ||
                 !SpellManager.HasSpell("Blackout Kick")) && //Low level
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Tiger Palm", Me.CurrentTarget, "TigerPowerBuff");
                                   LastTigerPowerBuff = DateTime.Now;
                               })
                );
        }

        #endregion

        #region TigerPowerBrew

        private Composite TigerPowerBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigerPalm &&
                SpellManager.HasSpell("Tiger Palm") &&
                LastTigerPowerBuff + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                SpellManager.HasSpell("Brewmaster Training") &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Tiger Palm", Me.CurrentTarget, "TigerPowerBrew");
                                   LastTigerPowerBuff = DateTime.Now;
                               })
                );
        }

        #endregion

        #region TigerPowerProc

        public DateTime LastTigerPowerProc;

        private Composite TigerPowerProc()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigerPalm &&
                SpellManager.HasSpell("Tiger Palm") &&
                LastTigerPowerProc + TimeSpan.FromMilliseconds(500) <= DateTime.Now &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.HasAura("Combo Breaker: Tiger Palm") &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Tiger Palm", Me.CurrentTarget, "TigerPowerProc");
                                   LastTigerPowerProc = DateTime.Now;
                               })
                );
        }

        #endregion

        #region TigereyeBrew

        private Composite TigereyeBrew()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TigereyeBrew &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.HasAura("Tigereye Brew") &&
                Me.GetAuraByName("Tigereye Brew").StackCount >= THSettings.Instance.TigereyeBrewStack &&
                Me.HasAura("Tiger Power") &&
                Me.GetAuraByName("Tiger Power").TimeLeft.TotalMilliseconds > 5000 &&
                Me.CurrentChi > 1 &&
                //(Me.CurrentChi >= 3 &&
                // GetSpellCooldown("Fists of Fury").TotalMilliseconds <= MyLatency &&
                // Me.CurrentEnergy <= THSettings.Instance.FistsofFuryEnergy &&
                // !Me.HasAura("Energizing Brew") ||
                // Me.CurrentChi >= 2 &&
                // GetSpellCooldown("Rising Sun Kick").TotalMilliseconds <= MyLatency ||
                // Me.HasAura("Energizing Brew")) &&
                Attackable(Me.CurrentTarget, 5) &&
                FacingOverride(Me.CurrentTarget),
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Tigereye Brew", Me, "TigereyeBrew");
                                   return RunStatus.Failure;
                               })
                );
        }

        #endregion

        #region TouchofDeath

        private Composite TouchofDeath()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TouchofDeath &&
                !CastingorGCDL() &&
                !Me.Mounted &&
                (Me.CurrentChi >= 3 ||
                 HasGlyph.Contains("123391")) &&
                Attackable(Me.CurrentTarget, 5) &&
                !Me.CurrentTarget.IsPet &&
                Me.CurrentTarget.CurrentHealth > Me.CurrentHealth - (Me.CurrentHealth*0.2) &&
                Me.HasAura("Death Note") &&
                GetSpellCooldown("Touch of Death").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Touch of Death", Me.CurrentTarget, "TouchofDeath");
                               })
                );
        }

        #endregion

        #region TouchofDeathPvP

        private Composite TouchofDeathPvP()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TouchofDeath &&
                SpellManager.HasSpell("Touch of Death") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                PvPPieceWindWalker >= 4 &&
                (Me.CurrentChi >= 3 ||
                 HasGlyph.Contains("123391")) &&
                Attackable(Me.CurrentTarget, 5) &&
                Me.CurrentTarget.IsPlayer &&
                Me.CurrentTarget.HealthPercent <= 10 &&
                GetSpellCooldown("Touch of Death").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Touch of Death", Me.CurrentTarget, "TouchofDeathPvP");
                               })
                );
        }

        #endregion

        #region TouchofKarma

        public WoWUnit UnitTouchofKarma;

        private bool GetUnitTouchofKarma()
        {
            UnitTouchofKarma = null;
            UnitTouchofKarma = (from unit in NearbyUnFriendlyPlayers
                                orderby unit.CurrentHealth ascending
                                where unit.Combat
                                where
                                    unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                    unit.IsTargetingMyRaidMember
                                where Attackable(unit, 5 + TouchofKarmaBonusRange)
                                where FacingOverride(unit)
                                where !InvulnerableSpell(unit)
                                select unit).FirstOrDefault();


            if (UnitTouchofKarma == null)
            {
                UnitTouchofKarma = (from unit in NearbyUnFriendlyUnits
                                    orderby unit.CurrentHealth descending
                                    where unit.Combat
                                    where !unit.IsPet
                                    where
                                        unit.IsTargetingMeOrPet || unit.IsTargetingMyPartyMember ||
                                        unit.IsTargetingMyRaidMember
                                    where Attackable(unit, 5 + TouchofKarmaBonusRange)
                                    where FacingOverride(unit)
                                    where !InvulnerableSpell(unit)
                                    select unit).FirstOrDefault();
            }
            return UnitTouchofKarma != null;
        }

        private Composite TouchofKarma()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.TouchofKarma &&
                SpellManager.HasSpell("Touch of Karma") &&
                !CastingorGCDL() &&
                !Me.Mounted &&
                Me.Combat &&
                Me.HealthPercent <= THSettings.Instance.TouchofKarmaHP &&
                Me.CurrentChi >= 2 &&
                GetSpellCooldown("Touch of Karma").TotalMilliseconds <= MyLatency &&
                GetUnitTouchofKarma() &&
                UnitTouchofKarma != null &&
                UnitTouchofKarma.IsValid,
                new Action(delegate
                               {
                                   SafelyFacingTarget(UnitTouchofKarma);
                                   CastSpell("Touch of Karma", UnitTouchofKarma, "TouchofKarma");
                                   UnitTouchofKarma = null;
                               })
                );
        }

        #endregion

        #region Uplift

        private double CountUnitUplift(double healthPercent)
        {
            return
                NearbyFriendlyPlayers.Where(
                    unit =>
                    MyAura("Renewing Mist", unit) &&
                    unit.HealthPercent <= healthPercent).Aggregate
                    <WoWUnit, double>(0, (current, unit) => current + 1);
        }

        private Composite Uplift()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Uplift &&
                SpellManager.HasSpell("Uplift") &&
                LastCastSpell != "Uplift" &&
                !Me.Mounted &&
                Me.Combat &&
                Me.CurrentChi >= 2 &&
                !CastingorGCDL() &&
                CountUnitUplift(THSettings.Instance.UpliftHP) >= THSettings.Instance.UpliftUnits,
                new Action(delegate
                               {
                                   if (THSettings.Instance.ThunderFocusTeaUplift &&
                                       CountUnitUplift(THSettings.Instance.ThunderFocusTeaUpliftHP) >=
                                       THSettings.Instance.ThunderFocusTeaUpliftUnits &&
                                       SpellManager.HasSpell("Thunder Focus Tea") &&
                                       GetSpellCooldown("Thunder Focus Tea").TotalMilliseconds <= MyLatency)
                                   {
                                       CastSpell("Thunder Focus Tea", Me, "ThunderFocusTeaUplift");
                                   }
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Uplift", Me, "Uplift");
                               })
                );
        }

        #endregion

        #region UseRacial

        private Composite UseRacial()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial && DebuffCCDuration(Me, 3000) &&
                    SpellManager.HasSpell("Every Man for Himself") &&
                    !Me.HasAura("Sap") &&
                    SpellManager.Spells["Every Man for Himself"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (THSettings.Instance.AutoTarget && Me.CurrentTarget == null &&
                                           ValidUnit(MyLastTarget))
                                       {
                                           MyLastTarget.Target();
                                       }

                                       if (DebuffCCBreakonDamage(Me) &&
                                           DebuffDot(Me))
                                       {
                                           Logging.Write(" - Waiting for Dot / Hand of Sacrifice break it!");
                                       }
                                       else
                                       {
                                           Logging.Write(" - Use: Every Man for Himself");
                                           CastSpell("Every Man for Himself", Me);
                                       }
                                       return RunStatus.Failure;
                                   })),
                //Stoneform
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial && Me.HealthPercent < THSettings.Instance.UrgentHeal &&
                    SpellManager.HasSpell("Stoneform") &&
                    SpellManager.Spells["Stoneform"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       {
                                           Logging.Write("Stoneform");
                                           CastSpell("Stoneform", Me);
                                       }
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial && Me.HealthPercent < THSettings.Instance.UrgentHeal &&
                    SpellManager.HasSpell("Gift of the Naaru") &&
                    SpellManager.Spells["Gift of the Naaru"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       {
                                           Logging.Write("Gift of the Naaru");
                                           CastSpell("Gift of the Naaru", Me);
                                       }
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial && Me.ManaPercent < THSettings.Instance.PriorityHeal &&
                    SpellManager.HasSpell("Arcane Torrent") && SpellManager.HasSpell("Holy Insight") &&
                    (Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) &&
                    SpellManager.Spells["Arcane Torrent"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       {
                                           Logging.Write("Arcane Torrent");
                                           CastSpell("Arcane Torrent", Me);
                                       }
                                       return RunStatus.Failure;
                                   }))
                );
        }

        #endregion

        #region UseTrinket

        private Composite UseTrinket()
        {
            return new PrioritySelector(
                //new Action(delegate { Logging.Write("UseTrinket Called"); }),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 1 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Cooldown");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 2 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Cooldown (Boss Only)");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 3 &&
                    THSettings.Instance.Burst &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Burst Mode");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 4 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1) &&
                    DebuffCCDuration(Me, 3000) && !Me.HasAura("Sap"),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Lose Control");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 5 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.HealthPercent < THSettings.Instance.Trinket1HP &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 6 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.HealthPercent < THSettings.Instance.Trinket1HP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Friendly Unit Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 7 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    Attackable(Me.CurrentTarget, 5) &&
                    !Me.CurrentTarget.IsPet &&
                    Me.CurrentTarget.HealthPercent < THSettings.Instance.Trinket1HP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Enemy Unit Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket1 == 8 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.ManaPercent < THSettings.Instance.Trinket1HP &&
                    Me.Inventory.Equipped.Trinket1 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket1),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                                                     " on Low Mana");
                                       StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                       return RunStatus.Failure;
                                   })),
                //Trinket 2
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 1 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Cooldown");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 2 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Cooldown (Boss Only)");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 3 &&
                    THSettings.Instance.Burst &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Burst Mode");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 4 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2) &&
                    DebuffCCDuration(Me, 3000) && !Me.HasAura("Sap"),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Lose Control");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 5 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.HealthPercent < THSettings.Instance.Trinket2HP &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 6 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.HealthPercent < THSettings.Instance.Trinket2HP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Friendly Unit Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 7 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    Attackable(Me.CurrentTarget, 5) &&
                    !Me.CurrentTarget.IsPet &&
                    Me.CurrentTarget.HealthPercent < THSettings.Instance.Trinket2HP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Enemy Unit Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.Trinket2 == 8 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.ManaPercent < THSettings.Instance.Trinket2HP &&
                    Me.Inventory.Equipped.Trinket2 != null &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Trinket2),
                    new Action(delegate
                                   {
                                       Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                                                     " on Low Mana");
                                       StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                       return RunStatus.Failure;
                                   }))
                );
        }

        #endregion

        #region UseProfession

        private Composite UseProfession()
        {
            return new PrioritySelector(
                //Engineering
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 1 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    Attackable(Me.CurrentTarget, 30) &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Gloves Buff Activated on Cooldown");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 2 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    //Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    Attackable(Me.CurrentTarget, 30) &&
                    Me.CurrentTarget.IsBoss &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Gloves Buff Activated on Cooldown (Boss Only)");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 3 &&
                    THSettings.Instance.Burst &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    Attackable(Me.CurrentTarget, 30) &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Gloves Buff Activated on Burst Mode");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 4 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands) &&
                    DebuffCCDuration(Me, 3000),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Gloves Buff Activated on Lose Control");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 5 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    Me.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: GLoves Buff Activated on Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 6 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.IsAlive &&
                    UnitHeal.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Gloves Buff Activated on Friendly Unit Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 7 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    Attackable(Me.CurrentTarget, 5) &&
                    !Me.CurrentTarget.IsPet &&
                    Me.CurrentTarget.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Gloves Buff Activated on Enemy Unit Low HP");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 8 &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.Inventory.Equipped.Hands != null &&
                    //(Me.Inventory.Equipped.Hands.GetEnchantment("Phase Fingers") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Synapse Springs") != null ||
                    // Me.Inventory.Equipped.Hands.GetEnchantment("Incendiary Fireworks Launcher") != null) &&
                    Me.ManaPercent < THSettings.Instance.ProfBuffHP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Gloves Buff Activated on Low Mana");
                                       StyxWoW.Me.Inventory.Equipped.Hands.Use();

                                       return RunStatus.Failure;
                                   })),
                //Herbalism
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 1 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Attackable(Me.CurrentTarget, 5) &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Cooldown");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 2 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Attackable(Me.CurrentTarget, 50) &&
                    Me.CurrentTarget.IsBoss &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Cooldown");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 3 &&
                    THSettings.Instance.Burst &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Burst Mode");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 4 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    DebuffCCDuration(Me, 3000) &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Lose Control");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 5 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Low HP");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 6 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.IsAlive &&
                    UnitHeal.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Friendly Unit Low HP");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 7 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Attackable(Me.CurrentTarget, 5) &&
                    !Me.CurrentTarget.IsPet &&
                    Me.CurrentTarget.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Enemy Unit Low HP");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 8 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    !Me.IsChanneling &&
                    Me.ManaPercent < THSettings.Instance.ProfBuffHP &&
                    GetSpellCooldown("Lifeblood").TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       Logging.Write("Use: Lifeblood Activated on Low Mana");
                                       CastSpell("Lifeblood", Me);
                                       return RunStatus.Failure;
                                   }))
                );
        }

        #endregion

        #region WriteDebug

        //private Composite WriteDebug(string message)
        //{
        //    return new Action(delegate
        //                          {
        //                              Logging.Write(message);
        //                              return RunStatus.Failure;
        //                          });
        //}

        #endregion

        #region WriteTargetInfo

        public DateTime LastWriteTargetInfo;

        //private Composite WriteTargetInfo()
        //{
        //    return new Action(delegate
        //                          {
        //                              if (Me.CurrentTarget != null &&
        //                                  LastWriteTargetInfo + TimeSpan.FromSeconds(3) < DateTime.Now)
        //                              {
        //                                  Logging.Write(Me.CurrentTarget.Name +
        //                                                " Entry: " + Me.CurrentTarget.Entry +
        //                                                " Dist: " + Math.Round(Me.CurrentTarget.Distance, 2) +
        //                                                " Dist2D: " + Math.Round(Me.CurrentTarget.Distance2D, 2) +
        //                                                " BoundingRadius: " +
        //                                                Math.Round(Me.CurrentTarget.BoundingRadius, 2) +
        //                                                " BoundingHeight: " +
        //                                                Math.Round(Me.CurrentTarget.BoundingHeight, 2) +
        //                                                " HoverHeight: " +
        //                                                Math.Round(Me.CurrentTarget.HoverHeight, 2) +
        //                                                " IsWithinMeleeRange: " + Me.CurrentTarget.IsWithinMeleeRange +
        //                                                " Attackable5: " + Attackable(Me.CurrentTarget, 5) +
        //                                                " FacingOverride: " + FacingOverride(Me.CurrentTarget) +
        //                                                " Rising Sun Kick: " +
        //                                                GetSpellCooldown("Rising Sun Kick").TotalMilliseconds);
        //                                  LastWriteTargetInfo = DateTime.Now;
        //                                  return RunStatus.Failure;
        //                              }
        //                              return RunStatus.Failure;
        //                          });
        //}

        #endregion

        #region ZenMeditation

        private Composite ZenMeditation()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ZenMed &&
                SpellManager.HasSpell("Zen Meditation") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.HealthPercent > THSettings.Instance.PriorityHeal &&
                (!Me.IsMoving || HasGlyph.Contains("120477")) && //Glyph of Zen Meditation
                !Me.HasAura("Zen Meditation") &&
                CountFriendlyNearNeedHeal(Me, 30, THSettings.Instance.ZenMedHP) >=
                THSettings.Instance.ZenMedUnit &&
                CountMeleeDPSTarget(Me) <= 0 &&
                GetSpellCooldown("Zen Meditation").TotalMilliseconds <= MyLatency,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(UnitHeal);
                                   CastSpell("Zen Meditation", Me, "ZenMeditation");
                               })
                );
        }

        #endregion

        #region ZenSphereMe

        private Composite ZenSphereMe()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ZenSphereMe &&
                SpellManager.HasSpell("Zen Sphere") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                Me.HealthPercent <= THSettings.Instance.ZenSphereMeHP &&
                !MyAura("Zen Sphere", Me),
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Zen Sphere", Me, "ZenSphereMe");
                               })
                );
        }

        #endregion

        #region ZenSphere

        public WoWUnit UnitGotZenSphere;

        private bool GetUnitGotZenSphere()
        {
            UnitGotZenSphere = null;
            UnitGotZenSphere = (from unit in NearbyFriendlyUnits
                                where MyAura("Zen Sphere", unit)
                                select unit).FirstOrDefault();
            return UnitGotZenSphere != null;
        }

        private Composite ZenSphere()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ZenSphere &&
                SpellManager.HasSpell("Zen Sphere") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                !GetUnitGotZenSphere() &&
                Healable(UnitHeal) &&
                UnitHeal.HealthPercent <= THSettings.Instance.ZenSphereHP,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Zen Sphere", UnitHeal, "ZenSphere");
                               })
                );
        }

        #endregion

        #region ZenSphereDetonate

        private Composite ZenSphereDetonate()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ZenSphereDetonate &&
                SpellManager.HasSpell("Zen Sphere") &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                Me.CurrentChi >= 2 &&
                GetUnitGotZenSphere() &&
                CountFriendlyNearNeedHeal(UnitGotZenSphere, 10, THSettings.Instance.ZenSphereDetonateHP) >=
                THSettings.Instance.ZenSphereDetonateUnit,
                new Action(delegate
                               {
                                   //SafelyFacingTarget(Me.CurrentTarget);
                                   CastSpell("Zen Sphere", UnitGotZenSphere, "ZenSphereDetonate");
                               })
                );
        }

        #endregion
    }
}